亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Android使用RecyclerView實現(xiàn)投票系統(tǒng)

 更新時間:2019年11月20日 11:24:21   作者:柯小帥  
這篇文章主要為大家詳細介紹了Android使用RecyclerView實現(xiàn)投票系統(tǒng),文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實例為大家分享了Android投票系統(tǒng)的具體代碼,供大家參考,具體內(nèi)容如下

一、創(chuàng)建一個fragment_vote_list.xml用來顯示投票的主頁面

(1)標題欄使用Toolbar
(2)投票區(qū)域可以滑動,使用RecyclerView實現(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首先需要在項目的build.gradle中添加相應的依賴庫才行。添加:implementation ‘com.android.support:recyclerview-v7:24.2.1'

界面效果:

二、創(chuàng)建一個item_vote.xml用來顯示投票的具體內(nèi)容

(1)主布局使用LinearLayout實現(xiàn),里面添加一個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>

界面效果:

三、創(chuàng)建一個投票信息實體類作為適配器的適配類型,新建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中定義的一個內(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)準備完畢,開始使用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" />

七、最終界面效果圖

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

  • Android 根據(jù)EditText搜索框ListView動態(tài)顯示數(shù)據(jù)

    Android 根據(jù)EditText搜索框ListView動態(tài)顯示數(shù)據(jù)

    這篇文章主要介紹了Android 根據(jù)EditText搜索框ListView動態(tài)顯示數(shù)據(jù)的相關資料,需要的朋友可以參考下
    2016-09-09
  • Android?Choreographer源碼詳細分析

    Android?Choreographer源碼詳細分析

    Choreographer的作用主要是配合Vsync,給上層App的渲染提供一個穩(wěn)定的Message處理的時機,也就是Vsync到來的時候,系統(tǒng)通過對Vsync信號周期的調(diào)整,來控制每一幀繪制操作的時機
    2022-08-08
  • Android編程實現(xiàn)簡單的UDP Client實例

    Android編程實現(xiàn)簡單的UDP Client實例

    這篇文章主要介紹了Android編程實現(xiàn)簡單的UDP Client,結合實例形式分析了Android實現(xiàn)UDP Clinet客戶端的實現(xiàn)技巧,需要的朋友可以參考下
    2016-04-04
  • Android Activity 橫豎屏切換的生命周期

    Android Activity 橫豎屏切換的生命周期

    這篇文章主要介紹了Android Activity 橫豎屏切換的生命周期的相關資料,需要的朋友可以參考下
    2016-04-04
  • Kotlin中使用Dagger2可能遇到的坑解決

    Kotlin中使用Dagger2可能遇到的坑解決

    在Android上創(chuàng)建去耦以及容易測試代碼的幾乎每位遲早都要訴諸Dagger,在Kotlin中設置Dagger有一些不同,所以下面這篇文章主要給大家介紹了關于Kotlin中使用Dagger2可能遇到的坑的解決方法,需要的朋友可以參考借鑒,下面隨著小編來一起學習學習吧。
    2017-11-11
  • Android實現(xiàn)朋友圈點贊列表

    Android實現(xiàn)朋友圈點贊列表

    這篇文章主要為大家詳細介紹了Android實現(xiàn)朋友圈點贊列表,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-11-11
  • Android學習教程之日歷庫使用(15)

    Android學習教程之日歷庫使用(15)

    這篇文章主要為大家詳細介紹了Android學習教程之日歷庫使用的相關資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2016-11-11
  • Android解讀Native崩潰棧信息的方法詳解

    Android解讀Native崩潰棧信息的方法詳解

    大部分的 Android 開發(fā)者使用的主要語言都是 Kotlin / Java,他們的崩潰棧信息非常清晰,也非常好定位到問題,本文小編給大家介紹了Android如何解讀Native崩潰棧信息,需要的朋友可以參考下
    2023-11-11
  • Android Studio OkHttpClient使用教程詳解

    Android Studio OkHttpClient使用教程詳解

    這篇文章主要介紹了Android Studio OkHttpClient使用教程詳解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2020-09-09
  • android開發(fā)教程之wifi開發(fā)示例

    android開發(fā)教程之wifi開發(fā)示例

    這篇文章主要介紹了android開發(fā)教程之wifi開發(fā)示例,需要的朋友可以參考下
    2014-03-03

最新評論