13

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

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

只有当我的项目比屏幕显示的少时才会出现这种情况。看看下面的这些屏幕截图。

互动前: 在此处输入图像描述

互动后: 在此处输入图像描述

添加项目的活动源代码:

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);
4

5 回答 5

6

尝试在 runOnUIThread() 方法中调用 notifyDataSetChanged() ,如下所示,它会像魅力一样工作。:)

                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        messageAdapter.notifyDataSetChanged();
                    }
                });
于 2014-01-02T06:49:27.670 回答
0

我不知道是什么导致了这个问题,但如果你没有找到一个合乎逻辑的解决方案,你可以尝试这样的事情:

  • onTouchEvent()每当您启动ListView.
  • 启动 ListView 后立即以编程方式向下滚动和备份。

ETC..

于 2012-07-16T09:01:10.847 回答
0

将 ListView 小部件添加到 layout.xml 并将列表内容添加到其中。不要使用 FrameLayout,因为它可能是问题的原因。它在触摸后更新内容,因此它所在的布局没有像 ListView 小部件那样实现正确的 onCreate() 设置。

于 2012-07-16T22:56:50.207 回答
0

添加新项目后,您是否在适配器上调用方法 notifyDataSetChanged()?这会导致列表视图在基础数据集更改时刷新其视图。

如果它仍然不起作用,请尝试 notifyDataSetInvalidated() 导致列表视图完全重绘。

于 2012-07-20T09:52:03.453 回答
0

解决了!!!

问题在于适配器试图重用相同的部分项目。不好!!!

将其更改为每次我们点击一​​个部分时都会膨胀部分项目!

于 2012-09-05T08:51:27.327 回答