导航抽屉滚动视图问题

问题描述 投票:0回答:2

我正在使用recyclerview,根据添加的日期有一组项目(例如,以A开头的字母表将在标题选项卡A下面,如联系人列表中所示)。问题出现的时候我向下滚动到底部,当我回到顶部时,标题选项卡没有正确分组..就像你拿Eg一样。上面给出..当我向下滚动到底部并且回到顶部时,不是所有以A开头的字母都在头部标签A下面...有些在b头标签下面...项目列表没有问题,它是正确的,只有头部标签。如果有人遇到过这种问题..请帮帮我。谢谢。

这是我在onBindViewHolder()中的代码

  class OrderAdapter extends RecyclerView.Adapter<OrderAdapter.MyViewHolder>
    {
    private List<OrderPosting> orderPostings;
        RecyclerView recyclerView;
    int index,size;
    String date;
    List<String> orderCountInsideAdapter;
        List<String> storeCount = new ArrayList<>();

    public OrderAdapter(List<OrderPosting> orderPostingList, int size, List<String> orderCount, List<String> storeCount)
    {
        this.orderPostings = orderPostingList;
        this.size = size;
        this.orderCountInsideAdapter = orderCount;
        this.storeCount = storeCount;
    }

    public class MyViewHolder extends RecyclerView.ViewHolder
    {
        TextView name, store, cost, date,statusColor,orderId;
        LinearLayout linearLayout,mainLayout;

        public MyViewHolder(View view)
        {
            super(view);

            name = (TextView) view.findViewById(R.id.personName);
            store = (TextView) view.findViewById(R.id.storeName);
            cost = (TextView) view.findViewById(R.id.totalCost);
            date = (TextView) view.findViewById(R.id.dateOrdered);
            linearLayout = (LinearLayout) view.findViewById(R.id.lastSeenLinearLayout);
            mainLayout = (LinearLayout) view.findViewById(R.id.mainLinearLayoutOrderHistory1);
            statusColor = (TextView) view.findViewById(R.id.status_color);
            orderId = (TextView) view.findViewById(R.id.order_id);
        }
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType)
    {
        View itemView = LayoutInflater.from(getActivity())
                .inflate(R.layout.order_history_items, parent, false);
        recyclerView = (RecyclerView) parent;

        return new OrderAdapter.MyViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(final MyViewHolder holder, final int position) {
            for (OrderPosting orderPosting: orderPostings)
            {

                if (Objects.equals(orderCountInsideAdapter.get(position), orderPosting.getOrderId()))
                {

                    DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
                    String date1 = df.format(new Date());

                    String dateTime = orderPosting.getDateAdded();
                    index = dateTime.indexOf(" ");
                    date = dateTime.substring(0, index);
                    Log.e("DateChange addes..////", date);

                    if(Objects.equals(tempDate, date))
                    {
                        Log.e("Temp date111/////",tempDate);
                    }
                    else
                    {
                        Log.e("Temp date222/////",tempDate);

                        holder.linearLayout.setVisibility(View.VISIBLE);

                        tempDate = date;

                        if(Objects.equals(date1, date))
                        {
                            holder.date.setText("Today");
                        }
                        else
                        {
                            SimpleDateFormat readFormat = new SimpleDateFormat("yyyy-mm-dd");
                            SimpleDateFormat writeFormat = new SimpleDateFormat("dd-mm-yyyy");

                            try {
                                convertDate = readFormat.parse(date);
                                Log.e("Changed date",""+writeFormat.format(convertDate));
                            } catch (ParseException e) {
                                e.printStackTrace();
                            }
                            holder.date.setText(writeFormat.format(convertDate));
                        }
                    }                  
        });
    }

    @Override
    public int getItemCount()
    {
        return size;
    }        
}

所以在上面的代码holder.linearLayout.setVisibility(View.VISIBLE);是头部标签布局..我相应地将其与日期分组。

android android-recyclerview navigation-drawer
2个回答
0
投票

问题出在if(Objects.equals(tempDate, date))。你的tempDate是一个全局变量,当你在底部时,你保存的日期以Z开头。

通过从底部滚动,最后一个值将首先出现 - >您将设置为假项目。


-1
投票

酷!这是一个分组的适配器。希望能帮助到你:

https://github.com/mathiasnielsen/mathirialdesign/blob/master/MaterialGuidelines/MathirialDesign/app/src/main/java/com/example/kirkegaard/mathirialdesign/Lists/GroupedListActivity.java

....如果链接不起作用:

package com.example.kirkegaard.mathirialdesign.Lists;

import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.example.kirkegaard.mathirialdesign.R;

import java.util.ArrayList;
import java.util.List;

public class GroupedListActivity extends AppCompatActivity {

    private List<CountriesData> countriesData;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_grouped_list);

        countriesData = new ArrayList<CountriesData>();

        MakeDummyData();

        SetupRecyclerView();
    }

    private void SetupRecyclerView()
    {
        RecyclerView recyclerView = (RecyclerView)findViewById(R.id.grouped_reyclerview);

        GroupedAdapter adapter = new GroupedAdapter(countriesData);

        recyclerView.setLayoutManager(new LinearLayoutManager(this));

        recyclerView.setAdapter(adapter);
    }

    private void MakeDummyData()
    {
        MakeDummyDataSection("A", 10);
        MakeDummyDataSection("C", 4);
        MakeDummyDataSection("G", 13);
        MakeDummyDataSection("H", 12);
        MakeDummyDataSection("Z", 3);
    }

    private void MakeDummyDataSection(String letter, int itemsCount)
    {
        List<String> cities = new ArrayList<String>();
        for (int i = 0; i < itemsCount; i++)
        {
            cities.add(letter + (i+1));
        }

        countriesData.add(new CountriesData(letter, cities));
    }

    private class CountriesData
    {
        public CountriesData(String letter, List<String> countries)
        {
            Letter = letter;
            Countries = countries;
        }

        public String Letter;

        public List<String> Countries;
    }

    private class GroupedAdapter extends RecyclerView.Adapter
    {
        private final int HEADER_TYPE = 0;
        private final int ITEM_TYPE = 1;

        private List<CountriesData> _data;
        private List<ItemInformation> _itemTypes;

        public GroupedAdapter(List<CountriesData> data)
        {
            _data = data;
            SetItemInformation();
        }

        private void SetItemInformation()
        {
            _itemTypes = new ArrayList<ItemInformation>();
            int categorySize = _data.size();
            for (int i = 0; i < categorySize; i++)
            {
                _itemTypes.add(new ItemInformation(HEADER_TYPE, i, 0));

                for (int j = 0; j < _data.get(i).Countries.size(); j++)
                {
                    _itemTypes.add(new ItemInformation(ITEM_TYPE, i, j));
                }
            }
        }

        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType)
        {
            if (viewType == HEADER_TYPE)
            {
                View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_group_header, parent, false);
                return new HeaderViewHolder(itemView);
            }
            else
            {
                View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_group_country, parent, false);
                return new CountryViewHolder(itemView);
            }
        }

        @Override
        public void onBindViewHolder(RecyclerView.ViewHolder holder, int position)
        {
            ItemInformation info = _itemTypes.get(position);
            if (holder instanceof HeaderViewHolder)
            {
                // Populate header data;
                String headerText = _data.get(info.GroupIndex).Letter;
                ((HeaderViewHolder)holder).PopulateWithData(headerText);
            }
            else
            {
                // Populate item data;
                String itemText = _data.get(info.GroupIndex).Countries.get(info.InnerIndex);
                ((CountryViewHolder)holder).PopulateWithData(itemText);
            }
        }

        @Override
        public int getItemViewType(int position)
        {
            ItemInformation info = _itemTypes.get(position);
            return info.ItemType;
        }

        @Override
        public int getItemCount()
        {

            int itemCount = 0;

            for (CountriesData countryWithSameLetter: _data)
            {
                // For header
                itemCount ++;

                // For content
                itemCount += countryWithSameLetter.Countries.size();
            }

            return itemCount;
        }

        private class ItemInformation
        {
            public ItemInformation(int itemType, int groupIndex, int innerIndex)
            {
                ItemType = itemType;
                GroupIndex = groupIndex;
                InnerIndex = innerIndex;
            }

            public int ItemType;

            public int GroupIndex;

            public int InnerIndex;
        }
    }

    private class HeaderViewHolder extends RecyclerView.ViewHolder
    {
        private TextView headerTextView;

        public HeaderViewHolder(View itemView)
        {
            super(itemView);

            headerTextView = (TextView)itemView.findViewById(R.id.group_header_textview);
        }

        public void PopulateWithData(String text)
        {
            headerTextView.setText(text);
        }
    }

    private class CountryViewHolder extends  RecyclerView.ViewHolder
    {
        private TextView itemTextView;

        public CountryViewHolder(View itemView)
        {
            super(itemView);

            itemTextView = (TextView)itemView.findViewById(R.id.group_country_textview);
        }


        public void PopulateWithData(String text)
        {
            itemTextView.setText(text);
        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.