需求:我们要点击进入一家店铺,根据不同的店铺,显示不同条数的子条目 如:消毒间,洗菜间等。。。这些都是或多或少的,所以需要动态添加:
首先自定义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如下:
<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>
下面是动态添加的代码:在他们的父布局一加载的时候开始动态添加:
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; } }这个的布局文件(main_dialog_layout)如下:
<?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>