Android uses recyclerview to implement voting system

This example shares the specific code of Android voting system for your reference. The specific contents are as follows

1、 Create a fragment_ Vote_ List.xml is used to display the main page of voting

(1) The title bar can slide in the voting area using the toolbar (2), which is implemented using recyclerview

<?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"
 xmlns:app="http://schemas.android.com/apk/res-auto"
 android:clickable="true"
 android:background="@color/backgroundColorWhite">
 <LinearLayout
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:background="@color/backgroundColorWhite"
  android:orientation="vertical">
  <android.support.v7.widget.Toolbar
   android:id="@+id/Vote_list_toolbar"
   android:layout_width="match_parent"
   android:layout_height="@dimen/toolbarHeight"
   android:background="@color/backgroundColorWhite"
   app:contentInsetStart="0dp">
   <RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <Button
     android:id="@+id/Vote_list_back_btn"
     android:layout_width="@dimen/titleBarBackWidth"
     android:layout_height="@dimen/titleBarBackHeight"
     android:layout_margin="@dimen/margin_min"
     android:layout_centerVertical="true"
     android:background="@drawable/titlebar_back"
     android:layout_marginLeft="@dimen/padding_20"
     />
    <TextView
     android:id="@+id/Vote_list_title_tv"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:layout_centerInParent="true"
     android:layout_gravity="center_vertical"
     android:text="投票"
     android:textColor="@color/textcolor_28282d"
     android:textSize="@dimen/textSizeMax"
     android:textStyle="bold"/>
   </RelativeLayout>
  </android.support.v7.widget.Toolbar>

  <android.support.v7.widget.RecyclerView
   android:id="@+id/Vote_list_recycleview"
   android:layout_width="match_parent"
   android:layout_height="match_parent">
  </android.support.v7.widget.RecyclerView>
 </LinearLayout>
</RelativeLayout>

Note: the font size and control width of the interface can be adjusted by yourself. To use recyclerview, you need to add the corresponding dependency Library in the build.gradle of the project. Add: implementation 'com. Android. Support: recyclerview-v7: 24.2.1'

Interface effect:

2、 Create an item_ Vote.xml is used to display the specific content of the vote

(1) The main layout is implemented by LinearLayout, and a textview is added to display the voting questions Check@R_ 197_ 2419 @ as a multi box for voting. (2) Load the current item into the main page of voting

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:orientation="vertical"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:background="@color/backgroundColorWhite"
 >
 <TextView
  android:id="@+id/item_Vote_question_tv"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:text="1.请问你支持一个决议?"
  android:textColor="@color/black"
  android:textSize="@dimen/item_Vote_question"
  android:layout_marginLeft="@dimen/padding_20" />
 <LinearLayout
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:background="@color/backgroundColorWhite"
  android:orientation="vertical"
  android:layout_margin="@dimen/padding_20">
  <Check@R_197_2419@
   android:id="@+id/item_Vote_answer1_cb"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:text="AAAAAA"
   android:textColor="@color/black"
   android:textSize="@dimen/item_Vote_answer" />
  <Check@R_197_2419@
   android:id="@+id/item_Vote_answer2_cb"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:text="BBBBBBB"
   android:textColor="@color/black"
   android:textSize="@dimen/item_Vote_answer" />
  <Check@R_197_2419@
   android:id="@+id/item_Vote_answer3_cb"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:text="BCCCCC"
   android:textColor="@color/black"
   android:textSize="@dimen/item_Vote_answer" />

 </LinearLayout>

</LinearLayout>

Interface effect:

3、 Create a voting information entity class as the adapter type, and create a voteinfo.java class.

public class VoteInfo {
 private String questionItem;
 private String[] answerItems;
 public VoteInfo(String questionItem,String[] answerItems){
  this.questionItem=questionItem;
  this.answerItems=answerItems;

 }
 public String getQuestionItem(){
  return questionItem;
 }
 public String[] getAnswerItems(){
  return answerItems;
 }
}

4、 Next, you need to prepare an adapter for recyclerview, create a new voteinfoadapter.java, make this adapter inherit from recyclerview.adapter, and specify the generic type as voteinfoadapter.viewholder. Among them, viewholder is an internal class defined in voteinfoadapter.

public class VoteInfoAdapter extends RecyclerView.Adapter<VoteInfoAdapter.ViewHolder> {

 private List<VoteInfo> mVoteInfoList;
 @Override
 public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent,int viewType) {
  View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.item_Vote,parent,false);
  ViewHolder holder=new ViewHolder(view);
  return holder;
 }

 @Override
 public void onBindViewHolder(@NonNull ViewHolder holder,int position) {
  VoteInfo VoteInfo=mVoteInfoList.get(position);
  holder.questionItem.setText(VoteInfo.getQuestionItem());
  holder.answerItem_1.setText(VoteInfo.getAnswerItems()[0]);
  holder.answerItem_2.setText(VoteInfo.getAnswerItems()[1]);
  holder.answerItem_3.setText(VoteInfo.getAnswerItems()[2]);

 }

 @Override
 public int getItemCount() {
  return mVoteInfoList.size();
 }

 static class ViewHolder extends RecyclerView.ViewHolder{
  TextView questionItem;
  Check@R_197_2419@ answerItem_1;
  Check@R_197_2419@ answerItem_2;
  Check@R_197_2419@ answerItem_3;
  public ViewHolder(View itemView) {
   super(itemView);
   questionItem=(TextView)itemView.findViewById(R.id.item_Vote_question_tv);
   answerItem_1=(Check@R_197_2419@)itemView.findViewById(R.id.item_Vote_answer1_cb);
   answerItem_2=(Check@R_197_2419@)itemView.findViewById(R.id.item_Vote_answer2_cb);
   answerItem_3=(Check@R_197_2419@)itemView.findViewById(R.id.item_Vote_answer3_cb);

  }
 }
 public VoteInfoAdapter(List<VoteInfo> VoteInfoList){
  mVoteInfoList=VoteInfoList;

 }
}

5、 The adapter is ready. Start using recyclerview and create a showvoteadapter.java class.

public class ShowVoteActivity extends BaseActivity{
 @BindView(R.id.Vote_list_recycleview)
 RecyclerView recyclerView;
 private List<VoteInfo> VoteInfoList=new ArrayList<VoteInfo>();

 @Override
 protected void onCreate(Bundle saveInstanceState) {
  super.onCreate(saveInstanceState);
  ScreenUtils.setContentViewWithOrientation(this,ScreenUtils.isPhone() ? R.layout.fragment_Vote_list : R.layout.fragment_Vote_list);
  initVoteInfo();
  linearlayoutmanager linearlayoutmanager=new linearlayoutmanager(this);
  recyclerView.setLayoutManager(linearlayoutmanager);
  VoteInfoAdapter VoteInfoAdapter=new VoteInfoAdapter(VoteInfoList);
  recyclerView.setAdapter(VoteInfoAdapter);
 }
 private void initVoteInfo(){
  VoteInfo Vote1=new VoteInfo("1.请问以下哪个答案最佳?",new String[]{"AAAAAA","BBBBBB","CCCCCC"});
  VoteInfoList.add(Vote1);
  VoteInfo Vote2=new VoteInfo("2.请问以下哪个答案最佳?","CCCCCC"});
  VoteInfoList.add(Vote2);
  VoteInfo Vote3=new VoteInfo("3.请问以下哪个答案最佳?","CCCCCC"});
  VoteInfoList.add(Vote3);
  VoteInfo Vote4=new VoteInfo("4.请问以下哪个答案最佳?","CCCCCC"});
  VoteInfoList.add(Vote4);
  VoteInfo Vote5=new VoteInfo("5.请问以下哪个答案最佳?","CCCCCC"});
  VoteInfoList.add(Vote5);
  VoteInfo Vote6=new VoteInfo("6.请问以下哪个答案最佳?","CCCCCC"});
  VoteInfoList.add(Vote6);
  VoteInfo Vote7=new VoteInfo("7.请问以下哪个答案最佳?","CCCCCC"});
  VoteInfoList.add(Vote7);
  VoteInfo Vote8=new VoteInfo("8.请问以下哪个答案最佳?","CCCCCC"});
  VoteInfoList.add(Vote8);
  VoteInfo Vote9=new VoteInfo("9.请问以下哪个答案最佳?","CCCCCC"});
  VoteInfoList.add(Vote9);
  VoteInfo Vote10=new VoteInfo("10.请问以下哪个答案最佳?","CCCCCC"});
  VoteInfoList.add(Vote10);
  VoteInfo Vote11=new VoteInfo("11.请问以下哪个答案最佳?","CCCCCC"});
  VoteInfoList.add(Vote11);

 }
}

6、 Showvoteactivity needs to be registered in androidmanifest.xml to start normally.

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

7、 Final interface rendering

The above is the whole content of this article. I hope it will help you in your study, and I hope you will support us a lot.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>