我正在創建一個ArrayAdapter爲我的GridView考慮頁眉和頁腳視圖。Android - 非常奇怪的行爲與GridView
activity_main.xml中:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<GridView
android:id="@+id/activity_main_gridview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:numColumns="4" />
</RelativeLayout>
的問題是,當我滾動到頂部或網格標題的底部/底部的觀點得到了網格項目,並在任何項目上點擊時,它可以追溯到原始位置(如果需要,我會放置屏幕截圖)
這是我的GridView適配器(params按順序:上下文,網格項目的arraylist(沒有頁眉/頁腳),所有第一行的頁眉視圖,行,gridview列數):
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mGridView = (GridView) findViewById(R.id.activity_main_gridview);
List<String> list = new ArrayList<String>();
for (int i = 0; i < 500; i++) {
list.add("item " + i);
}
View headerView = new View(this);
headerView.setMinimumHeight(70);
headerView.setBackgroundColor(Color.argb(255, 63, 81, 181));
GridHeaderFooterAdapter adapter =
new GridHeaderFooterAdapter(this, list, headerView, headerView, 4);
mGridView.setAdapter(adapter);
}
private final class GridHeaderFooterAdapter extends ArrayAdapter<String> {
private int mNumColumns;
private int mListSize;
private List<String> mList;
private View mHeaderView;
private View mFooterView;
private LayoutInflater mInflater;
public GridHeaderFooterAdapter(ActivityMain context, List<String> list, View headerView, View footerView, int numColumns) {
super(context, 0);
this.mNumColumns = numColumns;
this.mListSize = list.size();
this.mList = list;
this.mHeaderView = headerView;
this.mFooterView = footerView;
mInflater = context.getLayoutInflater();
}
@Override
public int getCount() {
int count = 0;
//headers
count += mNumColumns;
//list items
count += mListSize;
//footers
count += mNumColumns + (mNumColumns - mList.size() % mNumColumns);
Log.w("ActivityMain", "getCount() = " + count);
return count;
}
@Override
public String getItem(int position) {
//discard header items
return mList.get(position - mNumColumns);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
//headers
if (position < mNumColumns){
Log.w("ActivityMain", "headers: position = " + position);
return mHeaderView;
}
//listitems
if (position >= mNumColumns && position < mNumColumns + mListSize) {
Log.w("ActivityMain", "listitems: position = " + position);
LinearLayout ll = (LinearLayout) mInflater.inflate(R.layout.listitem_main, null);
TextView tv = (TextView) ll.findViewById(R.id.textView);
tv.setText(getItem(position));
return ll;
}
//footers
if (position >= mNumColumns + mListSize){
Log.w("ActivityMain", "footers: position = " + position);
/*TODO: some footers might be in the same row as listitem, which is wrong because Gridview uses last item of the row to determine row height... */
return mFooterView;
}
return null;
}
}
前SCROLLING: 後滾動:
正如你所看到的,目標是增加一個頭,以避免第一項是狀態欄
如果有問題的快照,最好.... –
@ i.n.e.f好的,完成 – BamsBamx
你正在用你的適配器做一些非常錯誤的事情。爲了幫助我解釋一下,首先你有什麼打算使用頁腳和頁眉?此外,這些屏幕截圖中的頁眉/頁腳視圖是什麼? –