Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> android 動態添加控件並實現每個子控件的點擊事件

android 動態添加控件並實現每個子控件的點擊事件

編輯:關於Android編程

需求:我們要點擊進入一家店鋪,根據不同的店鋪,顯示不同條數的子條目 如:消毒間,洗菜間等。。。這些都是或多或少的,所以需要動態添加:

首先自定義View(linearLayout):

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);
					}
				});

		
	}

}
上面用到的sub_right_top.xml如下:

	
	

    			

    			 
    			     
    			       		    
    		
    	




下面是動態添加的代碼:在他們的父布局一加載的時候開始動態添加:

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> 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>) 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;
	}

}
這個的布局文件(main_dialog_layout)如下:


   
   
        
    	

     
        
           
       

    





  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved