編輯:關於android開發
最近在項目中用到listview中再嵌套一個listview,兩層也有監聽,都沒有問題。其實,主要解決裡面那一層的listview的高度計算就可以,外面那一層listview自動計算。加上裡面那層展開的,就是高度。這種解決方案經常應用於朋友圈的評論。每一條說說,然後下面有評論。
這上面效果圖的監聽兩層都有,以及外面的button都可以。夠多樣化吧。
package com.nest.view; import android.content.Context; import android.util.AttributeSet; import android.widget.ListView; public class ChildListView extends ListView { public ChildListView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); // TODO Auto-generated constructor stub } public ChildListView(Context context, AttributeSet attrs) { super(context, attrs); // TODO Auto-generated constructor stub } public ChildListView(Context context) { super(context); // TODO Auto-generated constructor stub } @Override public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST); super.onMeasure(widthMeasureSpec, expandSpec); } }
package com.nest.adapt; import java.util.ArrayList; import java.util.List; import android.content.Context; import android.view.View; import android.view.ViewGroup; import android.view.View.OnClickListener; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.Button; import android.widget.ListView; import android.widget.TextView; import android.widget.Toast; import com.nest.activity.R; import com.nest.base.BaseObjectListAdapter; import com.nest.entity.Entity; import com.nest.entity.ShowEntity; /** * 父listview適配器 * @author mmsx * */ public class ParentAdapt extends BaseObjectListAdapter{ private ArrayListmChildList; public static int mParentItem = -1; public static boolean mbShowChild = false; public ParentAdapt(Context context, List datas) { super(context, datas); initData(); } private void initData() { mChildList = new ArrayList (); mChildList.clear(); for (int i = 0; i < 5; i++) { ShowEntity tempEntity = new ShowEntity(字標題 + String.valueOf(i), 內容); mChildList.add(tempEntity); } } @Override public View getView(int position, View convertView, ViewGroup parent) { ViewHolder vHolder = null; if (convertView == null) { vHolder = new ViewHolder(); convertView = mInflater.inflate(R.layout.activity_main_list_item, null); vHolder.textViewTitle = (TextView)convertView.findViewById(R.id.textView_1); vHolder.textViewContent= (TextView)convertView.findViewById(R.id.textView_2); vHolder.listViewItem = (ListView)convertView.findViewById(R.id.listView_child); vHolder.buttonStake = (Button)convertView.findViewById(R.id.button_1); convertView.setTag(vHolder); }else { vHolder = (ViewHolder) convertView.getTag(); } ShowEntity tempEntity = (ShowEntity) mDatas.get(position); vHolder.textViewTitle.setText(tempEntity.getTitle()); vHolder.textViewContent.setText(tempEntity.getContent()); //點擊那個彈出那個,如果已經彈出就收回子listview if (mParentItem == position && mbShowChild) { //子listview實在這裡加載數據的 ChildAdapt tempAdapt = new ChildAdapt(mContext, mChildList); vHolder.listViewItem.setAdapter(tempAdapt); vHolder.listViewItem.setVisibility(View.VISIBLE); //子listview的點擊監聽 vHolder.listViewItem.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView parent, View view, int position, long id) { Toast.makeText(mContext, Child Listview + String.valueOf(position), Toast.LENGTH_SHORT).show(); } }); }else { vHolder.listViewItem.setVisibility(View.GONE); } vHolder.buttonStake.setOnClickListener(new ParentButtonLisener()); //記住是那個button事件 vHolder.buttonStake.setTag(position); return convertView; } class ViewHolder{ TextView textViewTitle; TextView textViewContent; ListView listViewItem; Button buttonStake; } //父listview的button的監聽 private class ParentButtonLisener implements OnClickListener{ @Override public void onClick(View v) { Integer nPosition = (Integer) (v.getTag()); Toast.makeText(mContext, 提示 + String.valueOf(nPosition.intValue()), Toast.LENGTH_SHORT).show(); } } }
package com.nest.adapt; import java.util.List; import android.content.Context; import android.view.View; import android.view.ViewGroup; import android.widget.TextView; import com.nest.activity.R; import com.nest.base.BaseObjectListAdapter; import com.nest.entity.Entity; import com.nest.entity.ShowEntity; /** * 子listview適配器 * @author mmsx * */ public class ChildAdapt extends BaseObjectListAdapter{ public ChildAdapt(Context context, List datas) { super(context, datas); } @Override public View getView(int position, View convertView, ViewGroup parent) { ViewHolder vHolder = null; if (convertView == null) { vHolder = new ViewHolder(); convertView = mInflater.inflate(R.layout.activity_main_list_item_1, null); vHolder.textViewTitle = (TextView)convertView.findViewById(R.id.textView_1); vHolder.textViewContent= (TextView)convertView.findViewById(R.id.textView_2); convertView.setTag(vHolder); }else { vHolder = (ViewHolder) convertView.getTag(); } ShowEntity tempEntity = (ShowEntity) mDatas.get(position); vHolder.textViewTitle.setText(tempEntity.getTitle()); vHolder.textViewContent.setText(tempEntity.getContent()); return convertView; } class ViewHolder{ TextView textViewTitle; TextView textViewContent; } }
package com.nest.activity; import java.util.ArrayList; import com.nest.adapt.ParentAdapt; import com.nest.entity.ShowEntity; import android.os.Bundle; import android.view.View; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.ListView; import android.app.Activity; /** * listview嵌套listview實現效果 * @author mmsx * */ public class MainActivity extends Activity { private ParentAdapt mParentAdapt; private ArrayListmParentList; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initData(); initView(); } private void initData() { mParentList = new ArrayList (); mParentList.clear(); for (int i = 0; i < 20; i++) { ShowEntity tempEntity = new ShowEntity(標題 + String.valueOf(i), 內容); mParentList.add(tempEntity); } } private void initView() { ListView listView = (ListView)findViewById(R.id.listView1); mParentAdapt = new ParentAdapt(this, mParentList); listView.setAdapter(mParentAdapt); listView.setOnItemClickListener(new AdaptItemClick()); //長按listview就不寫示范了 // listView.setOnItemLongClickListener(listener); } //父listview的單擊事件的監聽 private class AdaptItemClick implements OnItemClickListener{ @Override public void onItemClick(AdapterView parent, View view, int position, long id) { if (ParentAdapt.mParentItem == position && ParentAdapt.mbShowChild) { ParentAdapt.mbShowChild = false; }else { ParentAdapt.mbShowChild = true; } ParentAdapt.mParentItem = position; mParentAdapt.notifyDataSetChanged(); } } }
僵屍進程的產生原因和避免方法僵屍進程的產生:當一個進程創建了一個子進程時,他們的運行時異步的。即父進程無法預知子進程會在什麼時候結束,那麼如果父進程很繁忙來不及wait
三種POST和GET的提交方式,postget提交 向服務器提交數據有兩種方式,post和get。兩者的區別主要有三點,
百度地圖可視化定位效果,可以輸入目的地定位。,可視化目的地登錄百度開發者帳號後下載sdk導入自己的工程中。 代碼如下: 1 package com.lixu.bai
自定義水平進度條樣式:黑色虛線,進度條虛線布局layout中使用: 下面為xml源代碼myprogress.xml: 1 <?xml v