這篇文章給大家分享的是有關(guān)Android中如何解使用RecyclerView實(shí)現(xiàn)投票系統(tǒng)的內(nèi)容。小編覺得挺實(shí)用的,因此分享給大家做個(gè)參考,一起跟隨小編過(guò)來(lái)看看吧。
成都創(chuàng)新互聯(lián)是一家專業(yè)提供
鳳山企業(yè)網(wǎng)站建設(shè),專注與
網(wǎng)站設(shè)計(jì)、網(wǎng)站制作、
H5高端網(wǎng)站建設(shè)、小程序制作等業(yè)務(wù)。10年已為鳳山眾多企業(yè)、政府機(jī)構(gòu)等服務(wù)。創(chuàng)新互聯(lián)專業(yè)的建站公司優(yōu)惠進(jìn)行中。
一、創(chuàng)建一個(gè)fragment_vote_list.xml用來(lái)顯示投票的主頁(yè)面
(1)標(biāo)題欄使用Toolbar(2)投票區(qū)域可以滑動(dòng),使用RecyclerView實(shí)現(xiàn)
注:界面字體大小以及控件寬度自行調(diào)整即可,使用RecyclerView首先需要在項(xiàng)目的build.gradle中添加相應(yīng)的依賴庫(kù)才行。添加:implementation ‘com.android.support:recyclerview-v7:24.2.1'
二、創(chuàng)建一個(gè)item_vote.xml用來(lái)顯示投票的具體內(nèi)容
(1)主布局使用LinearLayout實(shí)現(xiàn),里面添加一個(gè)TextView用來(lái)顯示投票的問(wèn)題,使用CheckBox作為投票的多選框。(2)將當(dāng)前的Item加載到投票的主頁(yè)面中
三、創(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; }}
四、接下來(lái)需要為RecyclerView準(zhǔn)備一個(gè)適配器,新建VoteInfoAdapter.java,讓這個(gè)適配器繼承自RecyclerView.Adapter,并將泛型指定為VoteInfoAdapter.ViewHolder。其中,ViewHolder是我們?cè)赩oteInfoAdapter中定義的一個(gè)內(nèi)部類。
public class VoteInfoAdapter extends RecyclerView.Adapter { private List 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 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 voteInfoList=new ArrayList(); @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)問(wèn)以下哪個(gè)答案很好?",new String[]{"AAAAAA","BBBBBB","CCCCCC"}); voteInfoList.add(vote1); VoteInfo vote2=new VoteInfo("2.請(qǐng)問(wèn)以下哪個(gè)答案很好?",new String[]{"AAAAAA","BBBBBB","CCCCCC"}); voteInfoList.add(vote2); VoteInfo vote3=new VoteInfo("3.請(qǐng)問(wèn)以下哪個(gè)答案很好?",new String[]{"AAAAAA","BBBBBB","CCCCCC"}); voteInfoList.add(vote3); VoteInfo vote4=new VoteInfo("4.請(qǐng)問(wèn)以下哪個(gè)答案很好?",new String[]{"AAAAAA","BBBBBB","CCCCCC"}); voteInfoList.add(vote4); VoteInfo vote5=new VoteInfo("5.請(qǐng)問(wèn)以下哪個(gè)答案很好?",new String[]{"AAAAAA","BBBBBB","CCCCCC"}); voteInfoList.add(vote5); VoteInfo vote6=new VoteInfo("6.請(qǐng)問(wèn)以下哪個(gè)答案很好?",new String[]{"AAAAAA","BBBBBB","CCCCCC"}); voteInfoList.add(vote6); VoteInfo vote7=new VoteInfo("7.請(qǐng)問(wèn)以下哪個(gè)答案很好?",new String[]{"AAAAAA","BBBBBB","CCCCCC"}); voteInfoList.add(vote7); VoteInfo vote8=new VoteInfo("8.請(qǐng)問(wèn)以下哪個(gè)答案很好?",new String[]{"AAAAAA","BBBBBB","CCCCCC"}); voteInfoList.add(vote8); VoteInfo vote9=new VoteInfo("9.請(qǐng)問(wèn)以下哪個(gè)答案很好?",new String[]{"AAAAAA","BBBBBB","CCCCCC"}); voteInfoList.add(vote9); VoteInfo vote10=new VoteInfo("10.請(qǐng)問(wèn)以下哪個(gè)答案很好?",new String[]{"AAAAAA","BBBBBB","CCCCCC"}); voteInfoList.add(vote10); VoteInfo vote11=new VoteInfo("11.請(qǐng)問(wèn)以下哪個(gè)答案很好?",new String[]{"AAAAAA","BBBBBB","CCCCCC"}); voteInfoList.add(vote11); }}
六、需要AndroidManifest.xml中注冊(cè)ShowVoteActivity,才能夠正常啟動(dòng)。
感謝各位的閱讀!關(guān)于“Android中如何解使用RecyclerView實(shí)現(xiàn)投票系統(tǒng)”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,讓大家可以學(xué)到更多知識(shí),如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到吧!
本文名稱:Android中如何解使用RecyclerView實(shí)現(xiàn)投票系統(tǒng)-創(chuàng)新互聯(lián)
本文鏈接:
http://weahome.cn/article/cciooh.html