RecyclerView并不总是显示数据(仅在少数情况下,甚至是错误的数据)

时间:2017-02-02 19:19:05

标签: android android-recyclerview

Android Studio中的RecyclerView存在一些问题。更新Android Studio后,它不再起作用了。它只是在少数情况下显示数据,但这些数据并不属于正确的点。我希望你能帮助我,因为我不知道出了什么问题。我将发布适配器的源代码,因为我认为问题在这里。 非常感谢,即使你无法提供帮助。

    import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
import java.util.ArrayList;

public class SatzungAdapter extends RecyclerView.Adapter<SatzungAdapter.ViewHolder> implements View.OnClickListener {
    private ArrayList<String> mDataset;
    private int expandedPosition = -1;
    public String Regeln[] = {HERE IS THE DATA STORED};

    public static class ViewHolder extends RecyclerView.ViewHolder {
        // each data item is just a string in this case
        public TextView txtHeader;
        public TextView txtFooter;
        LinearLayout ExpandArea;
        public TextView txtExpand;

        public ViewHolder(View v) {
            super(v);
            txtHeader = (TextView) v.findViewById(R.id.firstLine);
            ExpandArea = (LinearLayout) itemView.findViewById(R.id.ExpandArea);
            txtExpand = (TextView) v.findViewById(R.id.ExpandTxt);
        }
    }


    // Provide a suitable constructor (depends on the kind of dataset)
    public SatzungAdapter(ArrayList<String> myDataset) {
        mDataset = myDataset;
    }

    // Create new views (invoked by the layout manager)
    @Override
    public SatzungAdapter.ViewHolder onCreateViewHolder(ViewGroup parent,
                                                   int viewType) {
        // create a new view
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.satzung_text, parent, false);
        // set the view's size, margins, paddings and layout parameters
        ViewHolder vh = new ViewHolder(v);
        vh.itemView.setOnClickListener(SatzungAdapter.this);
        vh.itemView.setTag(vh);
        return vh;
    }

    // Replace the contents of a view (invoked by the layout manager)
    @Override
    public void onBindViewHolder(SatzungAdapter.ViewHolder holder, int position) {
        // - get element from your dataset at this position
        // - replace the contents of the view with that element
        final String name = mDataset.get(position);
        holder.txtHeader.setText(mDataset.get(position));
        if (position == expandedPosition) {
            holder.ExpandArea.setVisibility(View.VISIBLE);
        } else {
            holder.ExpandArea.setVisibility(View.GONE);
        }

        //holder.txtFooter.setText("Footer: " + mDataset.get(position));

    }

    @Override
    public void onClick(View view) {
        ViewHolder holder = (ViewHolder) view.getTag();
        String theString = mDataset.get(holder.getPosition());

        // Check for an expanded view, collapse if you find one
        if (expandedPosition >= 0) {
            int prev = expandedPosition;
            notifyItemChanged(prev);
        }
        // Set the current position to "expanded"
        expandedPosition = holder.getPosition();
        notifyItemChanged(expandedPosition);
        TextView iv = (TextView) view.findViewById(R.id.ExpandTxt);
        iv.setText(Regeln[expandedPosition]);
    }

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

}

1 个答案:

答案 0 :(得分:0)

覆盖getItemType(int position)

@Override
    public int getItemViewType(int position) {
        return position;
    }