效果图:
我们去写这个activity:
public class TestRecycleViewActivity extends Activity { private static final String TAG = TestRecycleViewActivity.class.getSimpleName(); private RecyclerView mListRecyclerView; private RecyclerAdapter mAdapter; private ArrayList<NormalItemInfo> mSymptomList = new ArrayList<>(); private String[] mDescriptionArray; public static void startActivity(Context context) { Intent intent = new Intent(); intent.setClass(context,TestRecycleViewActivity.class); context.startActivity(intent); } @Override public void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.recycle_list_acitivity); mListRecyclerView = (RecyclerView) findViewById(R.id.baby_recycler_view); mDescriptionArray = getResources().getStringArray(R.array.description); updateSymptomInfo(); mAdapter = new RecyclerAdapter(this, mSymptomList); mListRecyclerView.setLayoutManager(new LinearLayoutManager(this)); mListRecyclerView.setAdapter(mAdapter); RelativeLayout knowledge = (RelativeLayout) findViewById(R.id.knowledge); knowledge.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { } }); } public void updateSymptomInfo() { if (isFinishing()) { return; } for(int i=0;i<mDescriptionArray.length;i++) { NormalItemInfo symptomInfo = new NormalItemInfo(); symptomInfo.name = CalendarCont.mList[i]; symptomInfo.description = mDescriptionArray[i]; mSymptomList.add(symptomInfo); } } }
Adapter:
public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.SymptomHolder> { private Context mContext; private ArrayList<NormalItemInfo> mSymptomList = new ArrayList<>(); private LayoutInflater mLayoutInflater; public RecyclerAdapter(Context context, ArrayList<NormalItemInfo> symptomList) { mSymptomList = symptomList; mContext = context; mLayoutInflater = LayoutInflater.from(context); } @Override public SymptomHolder onCreateViewHolder(ViewGroup parent, int viewType) { View itemView = mLayoutInflater.inflate(R.layout.normal_recycleview_item, parent, false); return new SymptomHolder(itemView); } @Override public void onBindViewHolder(SymptomHolder holder, final int position) { holder.icon.setSelected(mSymptomList.get(position).isSelected); holder.nameTV.setText(mSymptomList.get(position).name); holder.description.setText(mSymptomList.get(position).description); holder.mView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if(mSymptomList.get(position).isSelected) { deSelectAll(); }else{ deSelectOther(position); } notifyDataSetChanged(); } }); } public void deSelectOther(final int position) { for (int i=0;i<mSymptomList.size();i++) { if(i == position) { mSymptomList.get(i).isSelected = true; }else{ mSymptomList.get(i).isSelected = false; } } } public void deSelectAll() { for (int i=0;i<mSymptomList.size();i++) { mSymptomList.get(i).isSelected = false; } } @Override public int getItemCount() { return mSymptomList.size(); } public class SymptomHolder extends RecyclerView.ViewHolder { private ImageView icon; private TextView nameTV; private TextView description; private View mView; private SymptomHolder(View itemView) { super(itemView); mView = itemView; icon = (ImageView)itemView.findViewById(R.id.symptom_icon); nameTV = (TextView)itemView.findViewById(R.id.symptom_name); description = (TextView)itemView.findViewById(R.id.description); } } }
主xml:
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <android.support.v7.widget.RecyclerView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/baby_recycler_view" android:layout_above="@+id/knowledge" android:layout_width="match_parent" android:layout_height="match_parent"/> <RelativeLayout android:id="@+id/knowledge" android:layout_width="match_parent" android:layout_height="44dp" android:background="#F5F5F5" android:layout_alignParentBottom="true"> <ImageView android:id="@+id/img" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:layout_marginLeft="16dp" android:src="@drawable/ca_img_calendar_knowledge"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:layout_marginLeft="8dp" android:layout_toRightOf="@id/img" android:text="小知识" android:textColor="#898B94" android:textSize="16sp"/> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_centerVertical="true" android:layout_marginRight="16dp" android:src="@drawable/ca_img_calendar_knowledge_enter"/> </RelativeLayout> </RelativeLayout>
listItem:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" > <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <ImageView android:id="@+id/symptom_icon" android:layout_width="19dp" android:layout_height="19dp" android:layout_marginLeft="21dp" android:layout_marginTop="23dp" android:background="@drawable/ca_calendar_symptom_selector"/> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="12dp" android:layout_marginTop="20dp" android:orientation="vertical"> <TextView android:id="@+id/symptom_name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:maxLines="1" android:text="只有经血" android:textColor="@color/pregnancy_color_181818" android:textSize="18sp"/> <TextView android:id="@+id/description" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:layout_marginRight="16dp" android:layout_marginTop="8dp" android:maxLines="2" android:text="此时处于月经期,无宫颈粘液分泌" android:textColor="@color/comm_text" android:textSize="13sp"/> </LinearLayout> </LinearLayout> <View android:layout_width="match_parent" android:layout_height="0.5dp" android:layout_marginTop="16dp" android:layout_marginRight="16dp" android:layout_marginLeft="16dp" android:background="@color/ca_line_color"/> </LinearLayout>
代码在:https://github.com/nickgao1986/StepSport