Android使用RecyclerView实现投票系统

前端之家收集整理的这篇文章主要介绍了Android使用RecyclerView实现投票系统前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

本文实例为大家分享了Android投票系统的具体代码,供大家参考,具体内容如下

一、创建一个fragment_vote_list.xml用来显示投票的主页面

(1)标题栏使用Toolbar
(2)投票区域可以滑动,使用RecyclerView实现

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. xmlns:app="http://schemas.android.com/apk/res-auto"
  6. android:clickable="true"
  7. android:background="@color/backgroundColorWhite">
  8. <LinearLayout
  9. android:layout_width="match_parent"
  10. android:layout_height="match_parent"
  11. android:background="@color/backgroundColorWhite"
  12. android:orientation="vertical">
  13. <android.support.v7.widget.Toolbar
  14. android:id="@+id/vote_list_toolbar"
  15. android:layout_width="match_parent"
  16. android:layout_height="@dimen/toolbarHeight"
  17. android:background="@color/backgroundColorWhite"
  18. app:contentInsetStart="0dp">
  19. <RelativeLayout
  20. android:layout_width="match_parent"
  21. android:layout_height="match_parent">
  22. <Button
  23. android:id="@+id/vote_list_back_btn"
  24. android:layout_width="@dimen/titleBarBackWidth"
  25. android:layout_height="@dimen/titleBarBackHeight"
  26. android:layout_margin="@dimen/margin_min"
  27. android:layout_centerVertical="true"
  28. android:background="@drawable/titlebar_back"
  29. android:layout_marginLeft="@dimen/padding_20"
  30. />
  31. <TextView
  32. android:id="@+id/vote_list_title_tv"
  33. android:layout_width="wrap_content"
  34. android:layout_height="wrap_content"
  35. android:layout_centerInParent="true"
  36. android:layout_gravity="center_vertical"
  37. android:text="投票"
  38. android:textColor="@color/textcolor_28282d"
  39. android:textSize="@dimen/textSizeMax"
  40. android:textStyle="bold"/>
  41. </RelativeLayout>
  42. </android.support.v7.widget.Toolbar>
  43.  
  44. <android.support.v7.widget.RecyclerView
  45. android:id="@+id/vote_list_recycleview"
  46. android:layout_width="match_parent"
  47. android:layout_height="match_parent">
  48. </android.support.v7.widget.RecyclerView>
  49. </LinearLayout>
  50. </RelativeLayout>

注:界面字体大小以及控件宽度自行调整即可,使用RecyclerView首先需要在项目的build.gradle中添加相应的依赖库才行。添加:implementation ‘com.android.support:recyclerview-v7:24.2.1'

界面效果

Android使用RecyclerView实现投票系统


二、创建一个item_vote.xml用来显示投票的具体内容

(1)主布局使用LinearLayout实现,里面添加一个TextView用来显示投票的问题,使用CheckBox作为投票的多选框。
(2)将当前的Item加载到投票的主页面

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:orientation="vertical"
  4. android:layout_width="match_parent"
  5. android:layout_height="wrap_content"
  6. android:background="@color/backgroundColorWhite"
  7. >
  8. <TextView
  9. android:id="@+id/item_vote_question_tv"
  10. android:layout_width="wrap_content"
  11. android:layout_height="wrap_content"
  12. android:text="1.请问你支持哪一个决议?"
  13. android:textColor="@color/black"
  14. android:textSize="@dimen/item_vote_question"
  15. android:layout_marginLeft="@dimen/padding_20" />
  16. <LinearLayout
  17. android:layout_width="wrap_content"
  18. android:layout_height="wrap_content"
  19. android:background="@color/backgroundColorWhite"
  20. android:orientation="vertical"
  21. android:layout_margin="@dimen/padding_20">
  22. <CheckBox
  23. android:id="@+id/item_vote_answer1_cb"
  24. android:layout_width="wrap_content"
  25. android:layout_height="wrap_content"
  26. android:text="AAAAAA"
  27. android:textColor="@color/black"
  28. android:textSize="@dimen/item_vote_answer" />
  29. <CheckBox
  30. android:id="@+id/item_vote_answer2_cb"
  31. android:layout_width="wrap_content"
  32. android:layout_height="wrap_content"
  33. android:text="BBBBBBB"
  34. android:textColor="@color/black"
  35. android:textSize="@dimen/item_vote_answer" />
  36. <CheckBox
  37. android:id="@+id/item_vote_answer3_cb"
  38. android:layout_width="wrap_content"
  39. android:layout_height="wrap_content"
  40. android:text="BCCCCC"
  41. android:textColor="@color/black"
  42. android:textSize="@dimen/item_vote_answer" />
  43.  
  44. </LinearLayout>
  45.  
  46. </LinearLayout>

界面效果

Android使用RecyclerView实现投票系统


三、创建一个投票信息实体类作为适配器的适配类型,新建VoteInfo.java类。

  1. public class VoteInfo {
  2. private String questionItem;
  3. private String[] answerItems;
  4. public VoteInfo(String questionItem,String[] answerItems){
  5. this.questionItem=questionItem;
  6. this.answerItems=answerItems;
  7.  
  8. }
  9. public String getQuestionItem(){
  10. return questionItem;
  11. }
  12. public String[] getAnswerItems(){
  13. return answerItems;
  14. }
  15. }

四、接下来需要为RecyclerView准备一个适配器,新建VoteInfoAdapter.java,让这个适配器继承自RecyclerView.Adapter,并将泛型指定为VoteInfoAdapter.ViewHolder。其中,ViewHolder是我们在VoteInfoAdapter中定义的一个内部类。

  1. public class VoteInfoAdapter extends RecyclerView.Adapter<VoteInfoAdapter.ViewHolder> {
  2.  
  3. private List<VoteInfo> mVoteInfoList;
  4. @Override
  5. public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent,int viewType) {
  6. View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.item_vote,parent,false);
  7. ViewHolder holder=new ViewHolder(view);
  8. return holder;
  9. }
  10.  
  11. @Override
  12. public void onBindViewHolder(@NonNull ViewHolder holder,int position) {
  13. VoteInfo voteInfo=mVoteInfoList.get(position);
  14. holder.questionItem.setText(voteInfo.getQuestionItem());
  15. holder.answerItem_1.setText(voteInfo.getAnswerItems()[0]);
  16. holder.answerItem_2.setText(voteInfo.getAnswerItems()[1]);
  17. holder.answerItem_3.setText(voteInfo.getAnswerItems()[2]);
  18.  
  19. }
  20.  
  21. @Override
  22. public int getItemCount() {
  23. return mVoteInfoList.size();
  24. }
  25.  
  26. static class ViewHolder extends RecyclerView.ViewHolder{
  27. TextView questionItem;
  28. CheckBox answerItem_1;
  29. CheckBox answerItem_2;
  30. CheckBox answerItem_3;
  31. public ViewHolder(View itemView) {
  32. super(itemView);
  33. questionItem=(TextView)itemView.findViewById(R.id.item_vote_question_tv);
  34. answerItem_1=(CheckBox)itemView.findViewById(R.id.item_vote_answer1_cb);
  35. answerItem_2=(CheckBox)itemView.findViewById(R.id.item_vote_answer2_cb);
  36. answerItem_3=(CheckBox)itemView.findViewById(R.id.item_vote_answer3_cb);
  37.  
  38. }
  39. }
  40. public VoteInfoAdapter(List<VoteInfo> voteInfoList){
  41. mVoteInfoList=voteInfoList;
  42.  
  43. }
  44. }

五、适配器已经准备完毕,开始使用RecyclerView,新建一个ShowVoteAdapter.java类。

  1. public class ShowVoteActivity extends BaseActivity{
  2. @BindView(R.id.vote_list_recycleview)
  3. RecyclerView recyclerView;
  4. private List<VoteInfo> voteInfoList=new ArrayList<VoteInfo>();
  5.  
  6. @Override
  7. protected void onCreate(Bundle saveInstanceState) {
  8. super.onCreate(saveInstanceState);
  9. ScreenUtils.setContentViewWithOrientation(this,ScreenUtils.isPhone() ? R.layout.fragment_vote_list : R.layout.fragment_vote_list);
  10. initVoteInfo();
  11. LinearLayoutManager linearLayoutManager=new LinearLayoutManager(this);
  12. recyclerView.setLayoutManager(linearLayoutManager);
  13. VoteInfoAdapter voteInfoAdapter=new VoteInfoAdapter(voteInfoList);
  14. recyclerView.setAdapter(voteInfoAdapter);
  15. }
  16. private void initVoteInfo(){
  17. VoteInfo vote1=new VoteInfo("1.请问以下哪个答案最佳?",new String[]{"AAAAAA","BBBBBB","CCCCCC"});
  18. voteInfoList.add(vote1);
  19. VoteInfo vote2=new VoteInfo("2.请问以下哪个答案最佳?","CCCCCC"});
  20. voteInfoList.add(vote2);
  21. VoteInfo vote3=new VoteInfo("3.请问以下哪个答案最佳?","CCCCCC"});
  22. voteInfoList.add(vote3);
  23. VoteInfo vote4=new VoteInfo("4.请问以下哪个答案最佳?","CCCCCC"});
  24. voteInfoList.add(vote4);
  25. VoteInfo vote5=new VoteInfo("5.请问以下哪个答案最佳?","CCCCCC"});
  26. voteInfoList.add(vote5);
  27. VoteInfo vote6=new VoteInfo("6.请问以下哪个答案最佳?","CCCCCC"});
  28. voteInfoList.add(vote6);
  29. VoteInfo vote7=new VoteInfo("7.请问以下哪个答案最佳?","CCCCCC"});
  30. voteInfoList.add(vote7);
  31. VoteInfo vote8=new VoteInfo("8.请问以下哪个答案最佳?","CCCCCC"});
  32. voteInfoList.add(vote8);
  33. VoteInfo vote9=new VoteInfo("9.请问以下哪个答案最佳?","CCCCCC"});
  34. voteInfoList.add(vote9);
  35. VoteInfo vote10=new VoteInfo("10.请问以下哪个答案最佳?","CCCCCC"});
  36. voteInfoList.add(vote10);
  37. VoteInfo vote11=new VoteInfo("11.请问以下哪个答案最佳?","CCCCCC"});
  38. voteInfoList.add(vote11);
  39.  
  40. }
  41. }

六、需要AndroidManifest.xml中注册ShowVoteActivity,才能够正常启动。

  1. <activity
  2. android:name="com.inpor.fastmeetingcloud.activity.ShowVoteActivity"
  3. android:configChanges="keyboardHidden|orientation|screenSize"
  4. android:screenOrientation="portrait"
  5. android:windowSoftInputMode="stateAlwaysHidden|adjustPan" />

七、最终界面效果

Android使用RecyclerView实现投票系统


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。

猜你在找的Android相关文章