Android仿京東分類效果
更新時間:2018年06月22日 16:34:17 作者:jun_tong
這篇文章主要為大家詳細介紹了Android仿京東分類效果,具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了Android仿京東分類效果展示的具體代碼,供大家參考,具體內容如下
1.寫一個fragment
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class MyFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
View view = inflater.inflate(R.layout.myfragment, null);
TextView tv_title = (TextView) view.findViewById(R.id.tv_title);
//得到數據
PersionInfo info = (PersionInfo) getArguments().getSerializable("info");
tv_title.setText(info.getNameString());
return view;
}
}
2.寫一個實體類:
public class PersionInfo implements Serializable {
private String nameString;
private boolean chick; //標識
public PersionInfo(String nameString) {
this.nameString = nameString;
}
public String getNameString() {
return nameString;
}
public void setNameString(String nameString) {
this.nameString = nameString;
}
public boolean isChick() {
return chick;
}
public void setChick(boolean chick) {
this.chick = chick;
}
}
3.寫一個adapter
public class MyAdapter extends BaseAdapter {
private Context context;
private List<PersionInfo> listinfos;
public MyAdapter(Context context, List<PersionInfo> listinfos){
this.context =context;
this.listinfos = listinfos;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return listinfos.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return listinfos.get(position);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
convertView = LayoutInflater.from(context).inflate(R.layout.listview_item, null);
TextView tv = (TextView) convertView.findViewById(R.id.tv);
PersionInfo persionInfo = listinfos.get(position);
tv.setText(persionInfo.getNameString());
if (persionInfo.isChick()) {
convertView.setBackgroundResource(R.drawable.tongcheng_all_bg01);
} else {
convertView.setBackgroundColor(Color.parseColor("#f4f4f4"));
}
return convertView;
}
}
4.adapter所需的list_item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:minHeight="60dp"
android:gravity="center"
android:orientation="vertical" >
<TextView
android:id="@+id/tv"
android:layout_width="wrap_content"
android:layout_height="match_parent" />
</LinearLayout>
Maintivity
/**
*
* @author yihuanxing
*
*/
public class MainActivity extends FragmentActivity implements OnItemClickListener {
List<PersionInfo> listinfoInfos=new ArrayList<PersionInfo>();
private ListView listView;
private MyAdapter adapter;
private MyFragment myFragment;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
/**
* 初始化view
*/
private void initView() {
// TODO Auto-generated method stub
for (int i = 0; i < 10; i++) {
PersionInfo info=new PersionInfo("常用分類"+i);
listinfoInfos.add(info);
}
//
listView = (ListView) findViewById(R.id.listview);
//默認
listinfoInfos.get(0).setChick(true);
adapter = new MyAdapter(this, listinfoInfos);
listView.setAdapter(adapter);
listView.setOnItemClickListener(this);
//創(chuàng)建MyFragment對象
myFragment = new MyFragment();
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.fragment_container, myFragment);
Bundle mBundle=new Bundle();
mBundle.putSerializable("info", listinfoInfos.get(0));
myFragment.setArguments(mBundle);
fragmentTransaction.commit();
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
PersionInfo info = listinfoInfos.get(position);
for (int i = 0; i < listinfoInfos.size(); i++) {
if(listinfoInfos.get(i).getNameString().equals(info.getNameString())){
listinfoInfos.get(i).setChick(true);
}else {
listinfoInfos.get(i).setChick(false);
}
}
adapter.notifyDataSetChanged();
//
//創(chuàng)建MyFragment對象
myFragment = new MyFragment();
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.fragment_container, myFragment);
Bundle mBundle=new Bundle();
mBundle.putSerializable("info", listinfoInfos.get(position));
myFragment.setArguments(mBundle);
fragmentTransaction.commit();
}
}
6 main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<RelativeLayout
android:id="@+id/jzfw_top_layout"
android:layout_width="match_parent"
android:layout_height="42dp"
android:background="@drawable/home_title"
android:gravity="center_vertical" >
<TextView
android:id="@+id/jzfw_top_layout_02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="仿京東分類"
android:textColor="#ffffff"
android:textSize="20sp" />
</RelativeLayout>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#cdcdcd" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#fbfbfb"
android:orientation="horizontal" >
<ListView
android:id="@+id/listview"
android:layout_width="0dp"
android:layout_height="match_parent"
android:scrollbars="none"
android:layout_weight="1.0"
android:background="#f4f4f4" />
<View
android:layout_width="1dp"
android:layout_height="match_parent"
android:background="#cdcdcd" />
<FrameLayout
android:id="@+id/fragment_container"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="3.0" />
</LinearLayout>
</LinearLayout>
效果圖:

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
Android?ContentObserver?監(jiān)聽短信思路詳解
ContentObserver允許在Android中監(jiān)控特定數據的變化,可用于短信等應用的數據監(jiān)聽,開發(fā)者可通過繼承ContentObserver并實現onChange方法來定義當目標內容變化時的響應行為,感興趣的朋友一起看看吧2024-09-09
Android編程簡單實現ImageView點擊時背景圖修改的方法
這篇文章主要介紹了Android編程簡單實現ImageView點擊時背景圖修改的方法,涉及Android針對背景圖相關屬性設置的操作技巧,需要的朋友可以參考下2015-12-12
Google 開發(fā)Android MVP架構Demo深入解析
這篇文章主要為大家介紹了Google 開發(fā)Android MVP架構Demo深入解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-11-11
淺談Android Classloader動態(tài)加載分析
這篇文章主要介紹了淺談Android Classloader動態(tài)加載分析,詳細的介紹了ClassLoader概念、分類,具有一定的參考價值,有興趣的可以了解一下2018-03-03
使用User Agent分辨出Android設備類型的安全做法
這篇文章主要介紹了使用User Agent分辨出Android設備類型的安全做法,本文得出的結論是當你依據檢測UA來判斷Android手機設備,請同時檢查android和mobile兩個字符串,需要的朋友可以參考下2015-01-01

