欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android使用RecyclerView实现投票系统

程序员文章站 2023-09-06 16:10:16
本文实例为大家分享了android投票系统的具体代码,供大家参考,具体内容如下 一、创建一个fragment_vote_list.xml用来显示投票的主页面 (1)标题栏使用too...

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

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

(1)标题栏使用toolbar
(2)投票区域可以滑动,使用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>

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

界面效果:

Android使用RecyclerView实现投票系统

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

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

<?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">
  <checkbox
   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" />
  <checkbox
   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" />
  <checkbox
   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>

界面效果:

Android使用RecyclerView实现投票系统

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

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;
 }
}

四、接下来需要为recyclerview准备一个适配器,新建voteinfoadapter.java,让这个适配器继承自recyclerview.adapter,并将泛型指定为voteinfoadapter.viewholder。其中,viewholder是我们在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;
  checkbox answeritem_1;
  checkbox answeritem_2;
  checkbox answeritem_3;
  public viewholder(view itemview) {
   super(itemview);
   questionitem=(textview)itemview.findviewbyid(r.id.item_vote_question_tv);
   answeritem_1=(checkbox)itemview.findviewbyid(r.id.item_vote_answer1_cb);
   answeritem_2=(checkbox)itemview.findviewbyid(r.id.item_vote_answer2_cb);
   answeritem_3=(checkbox)itemview.findviewbyid(r.id.item_vote_answer3_cb);

  }
 }
 public voteinfoadapter(list<voteinfo> voteinfolist){
  mvoteinfolist=voteinfolist;

 }
}

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

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.请问以下哪个答案最佳?",new string[]{"aaaaaa","bbbbbb","cccccc"});
  voteinfolist.add(vote2);
  voteinfo vote3=new voteinfo("3.请问以下哪个答案最佳?",new string[]{"aaaaaa","bbbbbb","cccccc"});
  voteinfolist.add(vote3);
  voteinfo vote4=new voteinfo("4.请问以下哪个答案最佳?",new string[]{"aaaaaa","bbbbbb","cccccc"});
  voteinfolist.add(vote4);
  voteinfo vote5=new voteinfo("5.请问以下哪个答案最佳?",new string[]{"aaaaaa","bbbbbb","cccccc"});
  voteinfolist.add(vote5);
  voteinfo vote6=new voteinfo("6.请问以下哪个答案最佳?",new string[]{"aaaaaa","bbbbbb","cccccc"});
  voteinfolist.add(vote6);
  voteinfo vote7=new voteinfo("7.请问以下哪个答案最佳?",new string[]{"aaaaaa","bbbbbb","cccccc"});
  voteinfolist.add(vote7);
  voteinfo vote8=new voteinfo("8.请问以下哪个答案最佳?",new string[]{"aaaaaa","bbbbbb","cccccc"});
  voteinfolist.add(vote8);
  voteinfo vote9=new voteinfo("9.请问以下哪个答案最佳?",new string[]{"aaaaaa","bbbbbb","cccccc"});
  voteinfolist.add(vote9);
  voteinfo vote10=new voteinfo("10.请问以下哪个答案最佳?",new string[]{"aaaaaa","bbbbbb","cccccc"});
  voteinfolist.add(vote10);
  voteinfo vote11=new voteinfo("11.请问以下哪个答案最佳?",new string[]{"aaaaaa","bbbbbb","cccccc"});
  voteinfolist.add(vote11);

 }
}

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

<activity
 android:name="com.inpor.fastmeetingcloud.activity.showvoteactivity"
 android:configchanges="keyboardhidden|orientation|screensize"
 android:screenorientation="portrait"
 android:windowsoftinputmode="statealwayshidden|adjustpan" />

七、最终界面效果图

Android使用RecyclerView实现投票系统

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