Android使用RecyclerView實(shí)現(xiàn)投票系統(tǒng)
本文實(shí)例為大家分享了Android投票系統(tǒng)的具體代碼,供大家參考,具體內(nèi)容如下
一、創(chuàng)建一個(gè)fragment_vote_list.xml用來顯示投票的主頁面
(1)標(biāo)題欄使用Toolbar
(2)投票區(qū)域可以滑動(dòng),使用RecyclerView實(shí)現(xiàn)
<?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>
注:界面字體大小以及控件寬度自行調(diào)整即可,使用RecyclerView首先需要在項(xiàng)目的build.gradle中添加相應(yīng)的依賴庫才行。添加:implementation ‘com.android.support:recyclerview-v7:24.2.1'
界面效果:
二、創(chuàng)建一個(gè)item_vote.xml用來顯示投票的具體內(nèi)容
(1)主布局使用LinearLayout實(shí)現(xiàn),里面添加一個(gè)TextView用來顯示投票的問題,使用CheckBox作為投票的多選框。
(2)將當(dāng)前的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.請(qǐng)問你支持哪一個(gè)決議?" 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>
界面效果:
三、創(chuàng)建一個(gè)投票信息實(shí)體類作為適配器的適配類型,新建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準(zhǔn)備一個(gè)適配器,新建VoteInfoAdapter.java,讓這個(gè)適配器繼承自RecyclerView.Adapter,并將泛型指定為VoteInfoAdapter.ViewHolder。其中,ViewHolder是我們?cè)赩oteInfoAdapter中定義的一個(gè)內(nèi)部類。
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; } }
五、適配器已經(jīng)準(zhǔn)備完畢,開始使用RecyclerView,新建一個(gè)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.請(qǐng)問以下哪個(gè)答案最佳?",new String[]{"AAAAAA","BBBBBB","CCCCCC"}); voteInfoList.add(vote1); VoteInfo vote2=new VoteInfo("2.請(qǐng)問以下哪個(gè)答案最佳?",new String[]{"AAAAAA","BBBBBB","CCCCCC"}); voteInfoList.add(vote2); VoteInfo vote3=new VoteInfo("3.請(qǐng)問以下哪個(gè)答案最佳?",new String[]{"AAAAAA","BBBBBB","CCCCCC"}); voteInfoList.add(vote3); VoteInfo vote4=new VoteInfo("4.請(qǐng)問以下哪個(gè)答案最佳?",new String[]{"AAAAAA","BBBBBB","CCCCCC"}); voteInfoList.add(vote4); VoteInfo vote5=new VoteInfo("5.請(qǐng)問以下哪個(gè)答案最佳?",new String[]{"AAAAAA","BBBBBB","CCCCCC"}); voteInfoList.add(vote5); VoteInfo vote6=new VoteInfo("6.請(qǐng)問以下哪個(gè)答案最佳?",new String[]{"AAAAAA","BBBBBB","CCCCCC"}); voteInfoList.add(vote6); VoteInfo vote7=new VoteInfo("7.請(qǐng)問以下哪個(gè)答案最佳?",new String[]{"AAAAAA","BBBBBB","CCCCCC"}); voteInfoList.add(vote7); VoteInfo vote8=new VoteInfo("8.請(qǐng)問以下哪個(gè)答案最佳?",new String[]{"AAAAAA","BBBBBB","CCCCCC"}); voteInfoList.add(vote8); VoteInfo vote9=new VoteInfo("9.請(qǐng)問以下哪個(gè)答案最佳?",new String[]{"AAAAAA","BBBBBB","CCCCCC"}); voteInfoList.add(vote9); VoteInfo vote10=new VoteInfo("10.請(qǐng)問以下哪個(gè)答案最佳?",new String[]{"AAAAAA","BBBBBB","CCCCCC"}); voteInfoList.add(vote10); VoteInfo vote11=new VoteInfo("11.請(qǐng)問以下哪個(gè)答案最佳?",new String[]{"AAAAAA","BBBBBB","CCCCCC"}); voteInfoList.add(vote11); } }
六、需要AndroidManifest.xml中注冊(cè)ShowVoteActivity,才能夠正常啟動(dòng)。
<activity android:name="com.inpor.fastmeetingcloud.activity.ShowVoteActivity" android:configChanges="keyboardHidden|orientation|screenSize" android:screenOrientation="portrait" android:windowSoftInputMode="stateAlwaysHidden|adjustPan" />
七、最終界面效果圖
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持我們。
上一篇:Android Selector獲取焦點(diǎn)后文本背景修改的實(shí)現(xiàn)代碼
欄 目:Android
下一篇:Android使用美團(tuán)多渠道打包方案詳解
本文標(biāo)題:Android使用RecyclerView實(shí)現(xiàn)投票系統(tǒng)
本文地址:http://mengdiqiu.com.cn/a1/Android/9064.html
您可能感興趣的文章
- 01-10Android自定義View之繪制圓形頭像功能
- 01-10Android實(shí)現(xiàn)雙擊返回鍵退出應(yīng)用實(shí)現(xiàn)方法詳解
- 01-10android實(shí)現(xiàn)記住用戶名和密碼以及自動(dòng)登錄
- 01-10android實(shí)現(xiàn)簡單計(jì)算器功能
- 01-10Android 友盟第三方登錄與分享的實(shí)現(xiàn)代碼
- 01-10android實(shí)現(xiàn)指紋識(shí)別功能
- 01-10Emoji表情在Android JNI中的兼容性問題詳解
- 01-10Android實(shí)現(xiàn)圓形漸變加載進(jìn)度條
- 01-10android開發(fā)環(huán)境中SDK文件夾下的所需內(nèi)容詳解
- 01-10android異步消息機(jī)制 源碼層面徹底解析(1)


閱讀排行
本欄相關(guān)
- 01-10Android自定義View之繪制圓形頭像功能
- 01-10Android實(shí)現(xiàn)雙擊返回鍵退出應(yīng)用實(shí)現(xiàn)方
- 01-10android實(shí)現(xiàn)簡單計(jì)算器功能
- 01-10android實(shí)現(xiàn)記住用戶名和密碼以及自動(dòng)
- 01-10C++自定義API函數(shù)實(shí)現(xiàn)大數(shù)相乘算法
- 01-10Android 友盟第三方登錄與分享的實(shí)現(xiàn)代
- 01-10android實(shí)現(xiàn)指紋識(shí)別功能
- 01-10如何給Flutter界面切換實(shí)現(xiàn)點(diǎn)特效
- 01-10Android實(shí)現(xiàn)圓形漸變加載進(jìn)度條
- 01-10Emoji表情在Android JNI中的兼容性問題詳
隨機(jī)閱讀
- 08-05dedecms(織夢(mèng))副欄目數(shù)量限制代碼修改
- 01-10使用C語言求解撲克牌的順子及n個(gè)骰子
- 04-02jquery與jsp,用jquery
- 01-11ajax實(shí)現(xiàn)頁面的局部加載
- 01-10C#中split用法實(shí)例總結(jié)
- 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置
- 08-05DEDE織夢(mèng)data目錄下的sessions文件夾有什
- 08-05織夢(mèng)dedecms什么時(shí)候用欄目交叉功能?
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 01-10delphi制作wav文件的方法