Android 動(dòng)態(tài)添加view或item并獲取數(shù)據(jù)的實(shí)例
最近在做一項(xiàng)目,項(xiàng)目中用到了一個(gè)功能,要求是動(dòng)態(tài)Item,而且是多個(gè)的情況下,不過仔細(xì)的分析了下,都大同小異,做起來也很簡(jiǎn)單,在這里我只抽取出來做了一demo,也只做了一個(gè)動(dòng)態(tài)添加item,同時(shí)可以獲取所有添加和編輯Item上的數(shù)據(jù),先上圖:

我們先來分析一下這個(gè)demo:
兩個(gè)TextView和EditText,一個(gè)Button,一個(gè)星級(jí)評(píng)價(jià)RatingBar控件,布局完事…
activity_dynamic的布局,有可能會(huì)添加多個(gè),所以外面用ScrollView,因?yàn)槲覀兪谴怪狈较蛱砑?,所以使用LinearLayout做容器
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="10dp">
<LinearLayout
android:id="@+id/ll_addView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" />
<Button
android:id="@+id/btn_getData"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/ll_addView"
android:layout_marginTop="10dp"
android:background="@drawable/em_btn_green_selector"
android:text="獲取數(shù)據(jù)" />
</RelativeLayout>
</ScrollView>
再看看要添加的item_hotel_evaluate里面的布局:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/rl_hotelName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/editbox_background_normal">
<LinearLayout
android:id="@+id/rl_addHotel"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/tv_hotelName"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_weight="1"
android:text="酒店名稱:"
android:textSize="18sp" />
<EditText
android:id="@+id/ed_hotelName"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:background="@drawable/editbox_background_normal"
android:padding="5dp"
android:singleLine="true" />
<Button
android:id="@+id/btn_addHotel"
android:layout_width="0dp"
android:layout_height="30dp"
android:layout_weight="1"
android:background="@drawable/em_btn_green_selector"
android:text="+新增"
android:textColor="@color/white"
android:textSize="18sp" />
</LinearLayout>
<LinearLayout
android:id="@+id/ll_addHotelEvaluate"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/rl_addHotel"
android:layout_marginTop="5dp"
android:orientation="vertical">
<RelativeLayout
android:id="@+id/rl_hotelEvaluate"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/rl_addHotel"
android:layout_marginTop="5dp"
android:orientation="horizontal">
<TextView
android:id="@+id/tv_hotelServer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_centerVertical="true"
android:layout_gravity="center_vertical"
android:layout_marginLeft="5dp"
android:layout_weight="1"
android:text="服務(wù)評(píng)價(jià):"
android:textSize="18sp" />
<RatingBar
android:id="@+id/rb_hotel_evaluate"
style="@style/myRatingBar"
android:layout_width="wrap_content"
android:layout_height="20dp"
android:layout_toRightOf="@+id/tv_hotelServer"
android:numStars="5"
android:rating="0"
android:stepSize="1.0" />
</RelativeLayout>
<EditText
android:id="@+id/ed_hotelEvaluate"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/rl_server"
android:background="@drawable/editbox_background_normal"
android:singleLine="true" />
</LinearLayout>
</RelativeLayout>
布局好了,因?yàn)锳ctivity里面的代碼寫不是很多,直接上代碼了,然后在最后分析一下:
package com.bob.lucking.activity;
import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RatingBar;
import com.bob.lucking.R;
/**
* Created by bob on 2017/3/20.
*/
public class DynamicAddViewActivity extends Activity implements View.OnClickListener {
private String TAG = this.getClass().getSimpleName();
//裝在所有動(dòng)態(tài)添加的Item的LinearLayout容器
private LinearLayout addHotelNameView;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dynamic);
addHotelNameView = (LinearLayout) findViewById(R.id.ll_addView);
findViewById(R.id.btn_getData).setOnClickListener(this);
//默認(rèn)添加一個(gè)Item
addViewItem(null);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_addHotel://點(diǎn)擊添加按鈕就動(dòng)態(tài)添加Item
addViewItem(v);
break;
case R.id.btn_getData://打印數(shù)據(jù)
printData();
break;
}
}
/**
* Item排序
*/
private void sortHotelViewItem() {
//獲取LinearLayout里面所有的view
for (int i = 0; i < addHotelNameView.getChildCount(); i++) {
final View childAt = addHotelNameView.getChildAt(i);
final Button btn_remove = (Button) childAt.findViewById(R.id.btn_addHotel);
btn_remove.setText("刪除");
btn_remove.setTag("remove");//設(shè)置刪除標(biāo)記
btn_remove.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//從LinearLayout容器中刪除當(dāng)前點(diǎn)擊到的ViewItem
addHotelNameView.removeView(childAt);
}
});
//如果是最后一個(gè)ViewItem,就設(shè)置為添加
if (i == (addHotelNameView.getChildCount() - 1)) {
Button btn_add = (Button) childAt.findViewById(R.id.btn_addHotel);
btn_add.setText("+新增");
btn_add.setTag("add");
btn_add.setOnClickListener(this);
}
}
}
//添加ViewItem
private void addViewItem(View view) {
if (addHotelNameView.getChildCount() == 0) {//如果一個(gè)都沒有,就添加一個(gè)
View hotelEvaluateView = View.inflate(this, R.layout.item_hotel_evaluate, null);
Button btn_add = (Button) hotelEvaluateView.findViewById(R.id.btn_addHotel);
btn_add.setText("+新增");
btn_add.setTag("add");
btn_add.setOnClickListener(this);
addHotelNameView.addView(hotelEvaluateView);
//sortHotelViewItem();
} else if (((String) view.getTag()).equals("add")) {//如果有一個(gè)以上的Item,點(diǎn)擊為添加的Item則添加
View hotelEvaluateView = View.inflate(this, R.layout.item_hotel_evaluate, null);
addHotelNameView.addView(hotelEvaluateView);
sortHotelViewItem();
}
//else {
// sortHotelViewItem();
//}
}
//獲取所有動(dòng)態(tài)添加的Item,找到控件的id,獲取數(shù)據(jù)
private void printData() {
for (int i = 0; i < addHotelNameView.getChildCount(); i++) {
View childAt = addHotelNameView.getChildAt(i);
EditText hotelName = (EditText) childAt.findViewById(R.id.ed_hotelName);
RatingBar hotelEvaluateStart = (RatingBar) childAt.findViewById(R.id.rb_hotel_evaluate);
EditText hotelEvaluate = (EditText) childAt.findViewById(R.id.ed_hotelEvaluate);
Log.e(TAG, "酒店名稱:" + hotelName.getText().toString() + "-----評(píng)價(jià)星數(shù):"
+ (int) hotelEvaluateStart.getRating() + "-----服務(wù)評(píng)價(jià):" + hotelEvaluate.getText().toString());
}
}
}
最后我們來解讀一下代碼:
onCreate里面初始化控件并設(shè)置事件,同時(shí)我們默認(rèn)添加一條item,因?yàn)閍ddHotelNameView容器初始化時(shí)里面沒有子view,所以我們默認(rèn)給添加的方法傳null,
在addViewItem方法時(shí),里面有初始化并設(shè)置button方法,所以在onclick方法里面把事件的v傳入是為了做標(biāo)記,也就是設(shè)置tag,,在添加時(shí)會(huì)有兩種情況:
1.如果只有一條,我們只能顯示添加
2.有多條的情況下,如果點(diǎn)擊的是設(shè)置有tag為add標(biāo)記的添加,則添加
如果點(diǎn)擊刪除,在sortHotelViewItem方法里面已經(jīng)設(shè)置過刪除點(diǎn)擊事件,直接從內(nèi)存中刪除,
最后是獲取數(shù)據(jù),我們可以通過LinearLayout容器來遍歷addHotelNameView.getChildCount()獲取所有添加的item,然后找到控件的id去獲取所有添加的item數(shù)據(jù)。
再這里注釋一下:在addViewItem方法里面看到可以優(yōu)化,上傳資源時(shí)已經(jīng)打包好了,現(xiàn)在在這里用單行注釋掉了4行,添加第一個(gè)item時(shí)不需要排序的,還有就是else里面的是死代碼,下載資源的朋友些可以刪除這幾行。
以上這篇Android 動(dòng)態(tài)添加view或item并獲取數(shù)據(jù)的實(shí)例就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
- Android 通過API獲取數(shù)據(jù)庫(kù)中的圖片文件方式
- Android WorkManager使用以及源碼分析
- Android WorkManager實(shí)現(xiàn)后臺(tái)定時(shí)任務(wù)流程詳解
- Android?Jetpack庫(kù)重要組件WorkManager的使用
- Android開發(fā)Jetpack組件WorkManager用例詳解
- Android使用Kotlin API實(shí)踐WorkManager
- Android WorkManager淺談
- android kotlin集成WorkManager實(shí)現(xiàn)定時(shí)獲取數(shù)據(jù)的步驟
相關(guān)文章
Android WebView的使用方法及與JS 相互調(diào)用
這篇文章主要介紹了Android WebView的使用方法及與JS 相互調(diào)用的相關(guān)資料,WebView 是 Android 中一個(gè)非常實(shí)用的組​件, WebView 可以使得網(wǎng)頁(yè)輕松的內(nèi)嵌到app里,還可以直接跟js相互調(diào)用,需要的朋友可以參考下2017-07-07
Android編程實(shí)現(xiàn)為應(yīng)用添加菜單的方法
這篇文章主要介紹了Android編程實(shí)現(xiàn)為應(yīng)用添加菜單的方法,涉及Android菜單界面布局與功能實(shí)現(xiàn)的相關(guān)技巧,需要的朋友可以參考下2016-01-01
android使用surfaceview+MediaPlayer播放視頻
這篇文章主要為大家詳細(xì)介紹了android使用surfaceview+MediaPlayer播放視頻,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-11-11
Android 中API之Drawable資源詳解及簡(jiǎn)單實(shí)例
這篇文章主要介紹了Android 中API之Drawable資源詳解及簡(jiǎn)單實(shí)例的相關(guān)資料,需要的朋友可以參考下2017-05-05
Anroid ListView分組和懸浮Header實(shí)現(xiàn)方法
這篇文章主要介紹了Anroid ListView分組和懸浮Header實(shí)現(xiàn)方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。2016-11-11
Android 帶有彈出收縮動(dòng)畫的扇形菜單實(shí)例
本篇文章主要介紹了Android 帶有彈出收縮動(dòng)畫的扇形菜單實(shí)例,具有一定的參考價(jià)值,有興趣的可以了解一下2017-06-06

