android 動態新增控制元件並實現每個子控制元件的點選事件
阿新 • • 發佈:2019-01-22
需求:我們要點選進入一家店鋪,根據不同的店鋪,顯示不同條數的子條目 如:消毒間,洗菜間等。。。這些都是或多或少的,所以需要動態新增:
首先自定義View(linearLayout):
上面用到的sub_right_top.xml如下:package cn.qust.fang.widget; import io.vov.vitamio.MediaPlayer; import io.vov.vitamio.widget.MediaController; import io.vov.vitamio.widget.VideoView; import android.app.Activity; import android.content.ContentResolver; import android.content.ContentValues; import android.content.Context; import android.net.Uri; import android.util.AttributeSet; import android.view.LayoutInflater; import android.view.View; import android.widget.EditText; import android.widget.LinearLayout; import android.widget.TextView; import android.widget.Toast; import cn.qust.fang.CHANGLIANG.ChangLiang; import cn.qust.fang.DB.DBManager; import cn.qust.fang.domain.Shop; import cn.qust.fang.main.MvplayActivity123; import cn.qust.fang.main.R; public class RightTopChildsLayouts extends LinearLayout { Shop shop=null; private TextView text_name_sub,textview_mvurl; View view; MvplayActivity123 currentactivity; /** * 配菜間 消毒間什麼的 */ String zi_name; /**\ * 配菜間 消毒間的 的視訊url */ String zi_mvurl; public RightTopChildsLayouts(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); // TODO Auto-generated constructor stub } //自定義建構函式 public RightTopChildsLayouts(MvplayActivity123 context,Shop shop,String zi_name,String zi_mvurl){ super(context ); view = LayoutInflater.from(context).inflate(R.layout.sub_right_top, null); text_name_sub=(TextView) view.findViewById(R.id.textview1); /** * 存mv_url */ textview_mvurl=(TextView) view.findViewById(R.id.textview_gone); //把獲得的view載入到這個控制元件中 addView(view); this.currentactivity=context; this.shop=shop; this.zi_mvurl=zi_mvurl; this.zi_name=zi_name; text_name_sub.setText(zi_name); textview_mvurl.setText(zi_mvurl); view.setOnClickListener(new OnClickListener(){ //點選事件 @Override public void onClick(View v) { // TODO Auto-generated method stub if(!getZi_mvurl().equals("collect")){ //若不是收藏 就切換視訊 System.out.println("點選=====RightTopChildsLayouts==zi_name========"+getZi_name()); VideoView mVideoView = currentactivity.getmVideoView(); mVideoView.pause(); Uri uri = Uri.parse(getZi_mvurl()); mVideoView.setVideoURI(uri); mVideoView.start(); } else { /** * 在這裡進行收藏 如果已收藏 顯示已收藏 */ System.out.println("點選=收藏====RightTopChildsLayouts==zi_name========"+getZi_name()); if (getShop() == null) Toast.makeText(getContext(), "收藏失敗", Toast.LENGTH_SHORT).show(); if (!(new DBManager(getContext()).exits(getShop().getShop_id()))) { save2db(getShop()); } else { //Toast.makeText(context, "已收藏", Toast.LENGTH_SHORT).show(); text_name_sub.setText(R.string.haven_collected); } } } }); } private void save2db(Shop shop) { ContentResolver resolver = getContext().getContentResolver(); ContentValues values = new ContentValues(); // System.out.println("baocun11前--------"); values.put("shop_name", shop.getShopname()); values.put("address", shop.getAddress()); values.put("mv_url", shop.getMv_url()); values.put("logo_url", shop.getLogo_url()); values.put("msg", shop.getMsg()); values.put("shop_id", shop.getShop_id()); values.put("shop_phone", shop.getShop_phone()); Uri url = Uri .parse("content://fang.ContentProvider.MyContentProvider_media/" + ChangLiang.TABLE); resolver.insert(url, values); // System.out.println("baocun11成功-----------"); // Toast.makeText(getContext(), "收藏成功", Toast.LENGTH_SHORT).show(); } public Shop getShop() { return shop; } public void setShop(Shop shop) { this.shop = shop; } public String getZi_name() { return zi_name; } public void setZi_name(String zi_name) { this.zi_name = zi_name; } public String getZi_mvurl() { return zi_mvurl; } public void setZi_mvurl(String zi_mvurl) { this.zi_mvurl = zi_mvurl; } //這個可能不太合適 @Override public void setOnClickListener(OnClickListener l) { // TODO Auto-generated method stub super.setOnClickListener(l); // this.currentactivity.finish(); VideoView mVideoView = currentactivity.getmVideoView(); mVideoView.pause(); Uri uri = Uri.parse(zi_name); mVideoView.setVideoURI(uri); mVideoView.start(); mVideoView .setOnPreparedListener(new MediaPlayer.OnPreparedListener() { @Override public void onPrepared(MediaPlayer mediaPlayer) { // optional need Vitamio 4.0 mediaPlayer.setPlaybackSpeed(1.0f); } }); } }
<pre name="code" class="html"> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/container" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.aaaa.MainActivity" tools:ignore="MergeRootFrame"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" > <!-- <ImageView android:id="@+id/imageView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:layout_marginLeft="8dp" android:src="@drawable/mm_title_btn_compose_normal" /> --> <TextView android:id="@+id/textview1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="8dp" android:text="洗菜間" android:textColor="#fff" android:textSize="18sp" /> <TextView android:id="@+id/textview_gone" android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="8dp" android:text="隱藏" android:textColor="#fff" android:textSize="18sp" android:visibility="gone" /> </LinearLayout> </RelativeLayout>
下面是動態新增的程式碼:在他們的父佈局一載入的時候開始動態新增:
這個的佈局檔案(main_dialog_layout)如下:public class MainTopRightDialog extends Activity { // private MyDialog dialog; private LinearLayout layout; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main_top_right_dialog); // dialog=new MyDialog(this); layout = (LinearLayout) findViewById(R.id.main_dialog_layout); Shop shop1 = (Shop) getIntent().getSerializableExtra("shop_inmvplayer"); MvplayActivity123 mvplayActivity123 = MvplayActivity123.getinstance(); List<Map<String, Object>> mv_urls_list = null; String mv_urls = shop1.getMv_url(); if (!mv_urls.equals("null") && mv_urls != null && !mv_urls.equals("")) { try { mv_urls_list = (List<Map<String, Object>>) new ParseJson2List() .ParseJson2List(shop1.getMv_url().toString()); } catch (JSONException e) { // TODO Auto-generated catch block e.printStackTrace(); } } else { mv_urls_list = null; return; } RightTopChildsLayouts layout_childs = null; /** * 根據 zi_mvurl 區分是url 還是收藏 若是收藏就為 collect */ RightTopChildsLayouts layout_collect = null; /// for (int i = 0; i < mv_urls_list.size(); i++) { layout_childs = new RightTopChildsLayouts(mvplayActivity123, shop1, mv_urls_list.get(i).get("_skey").toString(), mv_urls_list.get(i).get("_svalue").toString()); if (layout_childs != null) { /* android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_alignParentTop="true"*/ layout.addView(layout_childs,130,LinearLayout.LayoutParams.WRAP_CONTENT); } } /** * 新增收藏 項 若收藏過顯示已收藏 */ layout_collect=new RightTopChildsLayouts(mvplayActivity123, shop1, (new DBManager(this).exits(shop1.getShop_id())?"已收藏":"收藏"), "collect"); layout.addView(layout_collect,130,LinearLayout.LayoutParams.WRAP_CONTENT); /* layout.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub Toast.makeText(getApplicationContext(), "提示:點選視窗外部關閉視窗!", Toast.LENGTH_SHORT).show(); } });*/ } @Override public boolean onTouchEvent(MotionEvent event) { finish(); return true; } }
<?xml version="1.0" encoding="UTF-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginTop="46dp"
>
<LinearLayout
android:id="@+id/main_dialog_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:orientation="vertical"
android:background="@drawable/title_function_bg" >
<!-- <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="3dp"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="8dp"
android:text="收藏"
android:textColor="#fff"
android:textSize="18sp"
/>
</LinearLayout>
-->
</LinearLayout>
</RelativeLayout>
</RelativeLayout>