代码最重要
package com.weidingqiang.custommooc.views.widgets; import android.content.Context; import android.util.AttributeSet; import android.view.LayoutInflater; import android.view.View; import android.widget.AdapterView; import android.widget.LinearLayout; import android.widget.ListView; import com.weidingqiang.custommooc.R; import com.weidingqiang.custommooc.models.LeftMenuVO; import com.weidingqiang.custommooc.views.adapters.LeftMenuAdapter; import com.weidingqiang.custommooc.views.interfaces.LeftMenuViewImp; /** * Created by weidingqiang on 15/11/26. */ public class LeftMenuView extends LinearLayout { private LeftMenuViewImp leftMenuViewImp; private ListView listView; private LeftMenuAdapter leftMenuAdapter; public void setLeftMenuViewImp(LeftMenuViewImp leftMenuViewImp) { this.leftMenuViewImp = leftMenuViewImp; } public LeftMenuView(Context context) { this(context, null); } public LeftMenuView(Context context, AttributeSet attrs) { super(context, attrs); LayoutInflater layoutInflater = LayoutInflater.from(context); layoutInflater.inflate(R.layout.left_menu_view,this,true); initView(); initData(); initEvent(); } private void initView(){ listView =(ListView) this.findViewById(R.id.left_listview); } private void initData(){ leftMenuAdapter = new LeftMenuAdapter(getContext()); listView.setAdapter(leftMenuAdapter); } private void initEvent(){ listView.setOnItemClickListener(new DrawerItemClickListener()); } /* The click listner for ListView in the navigation drawer */ private class DrawerItemClickListener implements ListView.OnItemClickListener { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { LeftMenuVO selectLeftMenuVO = (LeftMenuVO) leftMenuAdapter.leftMenuVOs.get(position); if(selectLeftMenuVO.isSelected()) return; for (int i = 0;i<leftMenuAdapter.getCount();i++) { LeftMenuVO leftMenuVO = (LeftMenuVO) leftMenuAdapter.leftMenuVOs.get(i); leftMenuVO.setSelected(i == position); } leftMenuViewImp.leftselectItem(position); leftMenuAdapter.notifyDataSetChanged(); } } }
adapter
package com.weidingqiang.custommooc.views.adapters; import android.content.Context; import android.graphics.Color; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.ImageView; import android.widget.TextView; import com.weidingqiang.custommooc.R; import com.weidingqiang.custommooc.models.LeftMenuVO; import com.weidingqiang.custommooc.utils.MoocUtils; import com.weidingqiang.custommooc.views.viewholder.LeftMenuViewHolder; import java.util.List; /** * Created by weidingqiang on 15/11/26. */ public class LeftMenuAdapter extends BaseAdapter { public List<LeftMenuVO> leftMenuVOs; private LayoutInflater mInflater; public LeftMenuAdapter(Context context) { super(); leftMenuVOs = MoocUtils.leftMenuData(); this.mInflater = LayoutInflater.from(context); } @Override public int getCount() { return leftMenuVOs.size(); } @Override public Object getItem(int position) { return leftMenuVOs.get(position); } @Override public long getItemId(int position) { return 0; } @Override public View getView(int position, View convertView, ViewGroup parent) { LeftMenuViewHolder holder = null; if (convertView == null) { holder=new LeftMenuViewHolder(); convertView = mInflater.inflate(R.layout.left_menu_list_item, null); holder.imageView = (ImageView)convertView.findViewById(R.id.left_list_item_imageview); holder.textView = (TextView)convertView.findViewById(R.id.left_list_item_textview); convertView.setTag(holder); }else { holder = (LeftMenuViewHolder)convertView.getTag(); } LeftMenuVO leftMenuVO = (LeftMenuVO)getItem(position); if(leftMenuVO.isSelected()){ holder.imageView.setBackgroundResource(leftMenuVO.getPressdrawable()); convertView.setBackgroundColor(Color.parseColor("#E1E2E4")); }else { holder.imageView.setBackgroundResource(leftMenuVO.getNormaldrawable()); convertView.setBackgroundColor(Color.parseColor("#FFFFFF")); } holder.textView.setSelected(leftMenuVO.isSelected()); holder.textView.setText(leftMenuVO.getLabel()); return convertView; } }
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/left_list_item_bg" > <ImageView android:layout_marginLeft="40dp" android:layout_marginTop="16dp" android:layout_marginBottom="16dp" android:id="@+id/left_list_item_imageview" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:layout_marginLeft="10dp" android:layout_marginTop="16dp" android:layout_marginBottom="16dp" android:id="@+id/left_list_item_textview" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="@drawable/left_list_text_color_item" /> </LinearLayout>
时间: 2024-10-11 13:09:45