gpt4 book ai didi

android - ListView 在与交互之前不会呈现所有项目

转载 作者:塔克拉玛干 更新时间:2023-11-01 21:45:16 24 4
gpt4 key购买 nike

我在使用 ListView 时遇到一个非常奇怪的问题。

我的适配器项中只有一部分呈现在屏幕上的 ListView 中,但是当我与 ListView 交互时(即尝试滚动它),所有项都正确呈现。

只有当我的元素少于屏幕显示的数量时,才会出现这个 fenonemon。看看下面的这些屏幕截图。

互动前: enter image description here

互动后: enter image description here

添加项目的 Activity 源代码:

String[] jRests = getResources().getStringArray(R.array.j_restaurants);
String[] lRests = getResources().getStringArray(R.array.l_restaurants);

items = new ArrayList<Object>();
items.add(getString(R.string.campus_j));
for(String item : jRests){
String[] val = item.split(",,,");
items.add(new FoodSectionListItem(new Restaurant(val[0], val[1], val[2], "")));
}
items.add(getString(R.string.campus_l));
for(String item : lRests){
String[] val = item.split(",,,");
items.add(new FoodSectionListItem(new Restaurant(val[0], val[1], val[2], "")));
}

getSupportActionBar().setDisplayHomeAsUpEnabled(true);
adapter = new BaseSectionAdapter(this, R.layout.list_item_fragment_header);
if(!isTabletView()){
adapter.setSelectedItem(-1);
}
adapter.setItems(items);

适配器代码:

public class BaseSectionAdapter extends AmazingAdapter {

private LayoutInflater inflater;
private int selectedItem = 0;
private List<Object> items;
private List<SectionItem> sections = new ArrayList<SectionItem>(10);
private List<Class> itemTypes = new ArrayList<Class>();
private List<Integer> sectionPositions = new ArrayList<Integer>();
private int listHeaderLayoutId;
private View headerView;

public static interface ISectionListItem {
public void setProps(View convertView, int position, int selectedItem);
public View getLayout(LayoutInflater inflater);
}

private class SectionItem implements Serializable {
private static final long serialVersionUID = -8930010937740160935L;
String text;
int position;

public SectionItem(String text, int position) {
this.text = text;
this.position = position;
}
}

public BaseSectionAdapter(Context context, int listHeaderLayoutId) {
this.listHeaderLayoutId = listHeaderLayoutId;
init(context);
}

public BaseSectionAdapter(Context context, int listHeaderLayoutId, List<Object> listItems) {
this.listHeaderLayoutId = listHeaderLayoutId;
init(context);
initListItems(listItems);
}

private void init(Context context) {
inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

public void setSelectedItem(int position) {
selectedItem = position;
}

// public List<ListItem> getItems() {
// return items;
// }

private void initListItems(List<Object> itemList) {
int curSection = -1;
//int curPosition = 0;
//curSection = 0;

this.items = itemList;
itemTypes.clear();
sections.clear();
sectionPositions.clear();



int listSize = itemList.size();
for(int i = 0; i < listSize; i++){
Object currentItem = items.get(i);
if(currentItem instanceof String){
sections.add(new SectionItem((String) currentItem,i));
curSection++;
}
if(!itemTypes.contains(currentItem.getClass())){
itemTypes.add(currentItem.getClass());
}


sectionPositions.add(curSection);
}

Log.d("test", "No of items = "+items.size());
Log.d("test", "No of itemtypes = "+itemTypes.size());
Log.d("test", "View type count = "+getViewTypeCount());
}

public void setItems(List<Object> itemList) {
initListItems(itemList);
}

public int getCount() {
return items==null?0:items.size();
}

@Override
public int getViewTypeCount(){
return (itemTypes.size() == 0)?1:itemTypes.size();
}

@Override
public int getItemViewType(int position){
return itemTypes.indexOf(items.get(position).getClass());
}

@Override
public boolean isEnabled(int position){
return !(items.get(position) instanceof String || items.get(position) instanceof EmptySectionListItem);
}

@Override
public Object getItem(int position) {
return items.get(position);
}

public long getItemId(int position) {
return position;
}

@Override
protected void onNextPageRequested(int page) {
// TODO Auto-generated method stub

}

@Override
protected void bindSectionHeader(View view, int position,
boolean displaySectionHeader) {
// TextView lSectionTitle = (TextView) view
// .findViewById(R.id.txt_list_header);
// if (displaySectionHeader) {
// lSectionTitle.setVisibility(View.VISIBLE);
// lSectionTitle
// .setText(getSections()[getSectionForPosition(position)]);
// } else {
// lSectionTitle.setVisibility(View.GONE);
// }
}

@Override
public View getAmazingView(int position, View convertView, ViewGroup parent) {
Object curItemObject = items.get(position);
boolean isHeader = (curItemObject instanceof String);

if(convertView == null){
if(isHeader && headerView != null){
convertView = headerView;
}else if(isHeader){
convertView = inflater.inflate(listHeaderLayoutId, null);
headerView = convertView;
}else{
convertView = ((ISectionListItem) curItemObject).getLayout(inflater);
}
}
if(isHeader){
TextView header = ((TextView)convertView.findViewById(R.id.txt_list_header));
header.setText((String)curItemObject);
}else{
((ISectionListItem)curItemObject).setProps(convertView, position, selectedItem);
}
return convertView;
}

@Override
public void configurePinnedHeader(View header, int position, int alpha) {

TextView textView = ((TextView)header.findViewById(R.id.txt_list_header));
textView.setText(getSections()[getSectionForPosition(position)]);
}

@Override
public int getPositionForSection(int section) {
if(section >= sections.size()){
return 0;
}

return sections.get(section).position;
}

@Override
public int getSectionForPosition(int position) {
return sectionPositions.get(position);
}

@Override
public String[] getSections() {
String[] res = new String[sections.size()];
for (int i = 0; i < res.length; i++) {
res[i] = sections.get(i).text;

}
return res;
}
}

布局代码:

LinearLayout layout = new LinearLayout(this);
layout.setOrientation(LinearLayout.HORIZONTAL);

FrameLayout listLayout = new FrameLayout(this);
LinearLayout.LayoutParams listParams = new LinearLayout.LayoutParams(0, FrameLayout.LayoutParams.MATCH_PARENT);
listParams.weight = 1;
listLayout.setId(LIST_FRAGMENT_VIEW_ID);

FrameLayout detailLayout = new FrameLayout(this);
LinearLayout.LayoutParams detailParams = new LinearLayout.LayoutParams(0, FrameLayout.LayoutParams.MATCH_PARENT);
detailParams.weight = 2;
detailLayout.setId(DETAIL_FRAGMENT_VIEW_ID);

layout.addView(listLayout, listParams);
layout.addView(detailLayout, detailParams);

if(savedInstanceState == null){

FragmentTransaction ft = getSupportFragmentManager().beginTransaction();

ft.add(listLayout.getId(), (Fragment) listFragment, TWO_PANEL_LIST_FRAGMENT_TAG);
ft.add(detailLayout.getId(), detailFragment);
ft.commit();

}
setContentView(layout);

最佳答案

尝试在 runOnUIThread() 方法中调用 notifyDataSetChanged() ,如下所示,它会非常有效。 :)

                runOnUiThread(new Runnable() {
@Override
public void run() {
messageAdapter.notifyDataSetChanged();
}
});

关于android - ListView 在与交互之前不会呈现所有项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11467653/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com