使用LinearLayout实现ListView,解决ListView和ScrollView滚动冲突
在項目中,我們常常會遇到一個ScrollView里面會嵌套ListView的情況,但往往你會發(fā)現(xiàn),ListView和ScrollView的滾動時間會有沖突問題,造成ListView不能完全顯示。雖然網(wǎng)上有給出解決方案,但事實上并不好用,并不完美。
public void setListViewHeightBasedOnChildren(ListView listView) {
?
??// 獲取ListView對應(yīng)的Adapter
??ListAdapter listAdapter = listView.getAdapter();
??if (listAdapter == null) {
???return;
??}
??int totalHeight = 0;
??for (int i = 0; i < listAdapter.getCount(); i++) { // listAdapter.getCount()返回數(shù)據(jù)項的數(shù)目
???View listItem = listAdapter.getView(i, null, listView);
???listItem.measure(0, 0); // 計算子項View 的寬高
???totalHeight += listItem.getMeasuredHeight(); // 統(tǒng)計所有子項的總高度
??}
??ViewGroup.LayoutParams params = listView.getLayoutParams();
??params.height = totalHeight
????+ (listView.getDividerHeight() * (listAdapter.getCount() - 1));
??// listView.getDividerHeight()獲取子項間分隔符占用的高度
??// params.height最后得到整個ListView完整顯示需要的高度
??listView.setLayoutParams(params);
?}
?
因此,為了徹底解決問題,就需要我們另辟途徑了,我們使用?LinearLayout 綁定 Adapter的方法來代替ListView
將 adapter 里面的 view 全部加入 LinearLayout 并設(shè)置回調(diào)監(jiān)聽,添加DataSetObserver監(jiān)聽adapter 的改變。
?
public class LinearLayoutForListView extends LinearLayout {private Adapter mAdapter;
private OnItemClickListener mOnItemClickListener;
private SparseArray<View> mViewHolders;
private AdapterDataSetObserver mDataSetObserver;
public OnItemClickListener getOnItemClickListener() {
return mOnItemClickListener;
}
public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
mOnItemClickListener = onItemClickListener;
}
public LinearLayoutForListView(Context context) {
super(context);
}
public LinearLayoutForListView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public LinearLayoutForListView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
public void setAdapter(Adapter adapter) {
this.mAdapter = adapter;
bindLinearLayout();
}
/** 綁定布局 */
private void bindLinearLayout() {
if (mAdapter == null) {
return;
}
setOrientation(LinearLayout.VERTICAL);
int count = mAdapter.getCount();
this.removeAllViews();
mViewHolders = new SparseArray<View>(count);
for (int i = 0; i < count; i++) {
final View v = mAdapter.getView(i, null, null);
final int tmp = i;
final Object obj = mAdapter.getItem(i);
mViewHolders.put(i,v);
// view 點擊事件觸發(fā)時回調(diào)我們自己的接口
v.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (mOnItemClickListener != null) {
mOnItemClickListener.onItemClicked(v, obj, tmp);
}
}
});
addView(v);
}
}
/**
*
* 回調(diào)接口
*/
public interface OnItemClickListener {
/**
* @param v 點擊的 view
* @param obj 點擊的 view 所綁定的對象
* @param position 點擊位置的 index
*/
public void onItemClicked(View v, Object obj, int position);
}
@Override
protected void onAttachedToWindow() {
super.onAttachedToWindow();
if(mAdapter != null && mDataSetObserver == null){
mDataSetObserver = new AdapterDataSetObserver();
mAdapter.registerDataSetObserver(mDataSetObserver);
}
}
@Override
protected void onDetachedFromWindow() {
super.onDetachedFromWindow();
if(mAdapter != null && mDataSetObserver != null){
mAdapter.unregisterDataSetObserver(mDataSetObserver);
}
}
private class AdapterDataSetObserver extends DataSetObserver {
@Override
public void onChanged() {
if(mAdapter!=null){
final int count = mAdapter.getCount();
for (int i = 0; i < count; i++){
mAdapter.getView(i,mViewHolders.get(i,null),null);
}
requestLayout();
}
}
}
}
調(diào)用
list.setAdapter(madapter);
list.setOnItemClickListener(new LinearLayoutForListView.OnItemClickListener() {
@Override
public void onItemClicked(View v, Object obj, int position) {
}
});
轉(zhuǎn)載于:https://www.cnblogs.com/lucktian/p/6478227.html
總結(jié)
以上是生活随笔為你收集整理的使用LinearLayout实现ListView,解决ListView和ScrollView滚动冲突的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 梦到纸箱子是什么意思
- 下一篇: 孕妇梦到吃李子预示着什么