編輯:關於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(); } } }
Android 自定義view模板並實現點擊事件的回調主要的目的就是仿老版QQ的一個界面做一個模板。然後實現點擊事件的回調。先看效果圖:步驟如下: 1.在res/valu
這兩天在回顧Android Service方面的知識,趁著記憶沒有消退之前,來總結一下。本文主要講解Service的基本概念與使用、跨進程調用Service、系統常見Se
??通過上一篇文件的分析,我們對Activity的啟動模式有了比較清晰的了解後,本篇我們將繼續對Activity啟動模式的相關參數和任務棧分析,接下來我們就繼續上一篇的問
我們有新的項目要進行開發了,一直想用用android studio。所以在新項目上,果斷使用。這裡是我將android studio項目share到svn倉庫的全過程。後