如何使用视图从我的适配器获取索引?

时间:2016-08-31 13:43:45

标签: android android-recyclerview adapter

很抱歉,如果这是令人费解的......

我正在尝试创建一个异步任务,在单击时更新回收器视图适配器中的值。我的问题是我无法从火车LiastArray获得一个特定的行,从查看返回点击其中一个RecyclerView行。

$ cd /home/test/Desktop/code/Project
$ git status
On branch master
Your branch is up-to-date with 'origin/master'.

$ ipython3

In [1]: import os, pygit2
In [2]: repo = pygit2.Repository(os.getcwd())

基本上,我有一个Train类,其中包含基本的平台,到达时间,目的地等字段,包括getter和setter。然后我用Train类的ArrayList设置一个适配器,然后将适配器分配给我的RecyclerView:

public void ArrivalTimeOnClickListener(View view) {
    pbArrivalTime = (ProgressBar)view.findViewById(R.id.pbArrivalTime);
    tvArrivalTime = (TextView)view.findViewById(R.id.tvArrivalTime);

    /**
     * GET ADAPTER INDEX HERE
     *
     * TRIED:
     * int trainIndex = rvTrainTimetable.getChildAdapterPosition(view.findViewById(R.id.rlTrainRoot));
     */

    new RefreshSingleArrivalTime().execute(view);
}

我的TrainAdapter定义如下:

private void InitializeTrainTimetable() {
    trains = new ArrayList<Train>();
    trains.add(new Train("Artarmion Platform 3", 7, "On time", "Ahfield", "15:01"));
    trains.add(new Train("TEST", 97, "TEST", "TEST", "TEST"));

    trainAdapter = new TrainAdapter(this, trains);

    rvTrainTimetable = (RecyclerView) findViewById(R.id.rvTrainTimetable);

    rvTrainTimetable.setLayoutManager(new LinearLayoutManager(getApplicationContext()));
    rvTrainTimetable.setItemAnimator(new DefaultItemAnimator());
    rvTrainTimetable.setAdapter(trainAdapter);
}

My Train Item布局在具有3个LinearLayouts的相对布局中定义:

public class TrainAdapter extends RecyclerView.Adapter<TrainAdapter.ViewHolder> {
    private ArrayList<Train> mTrains;

    public TrainAdapter(Context context, ArrayList<Train> trains){
        mTrains = trains;
    }

    /** Inflates xml layout file */
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.train_item, parent, false);

        return new ViewHolder(itemView);
    }

    /** Initialise and assign values for eah row in recycler view */
    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        Train train = mTrains.get(position);

        holder.mArrivalTime.setText(String.format(Constants.REMAINING_MINS, train.getArrivalTime()));
        holder.mPlatform.setText(train.getPlatform());
        // TOODO: get actual time
        holder.mTime.setText("14:14");
        holder.mStatus.setText(train.getStatus());
        holder.mDestinationTime.setText(train.getDestinationTime());
        holder.mDestination.setText(train.getDestination());
    }

    /** Returns number of items in list */
    @Override
    public int getItemCount() {
        return mTrains.size();
    }

    /** Initialises xml elements */
    class ViewHolder extends RecyclerView.ViewHolder {
        public TextView mArrivalTime;
        public TextView mPlatform;
        public TextView mTime;
        public TextView mStatus;
        public TextView mDestinationTime;
        public TextView mDestination;

        /** Initialises xml elements */
        public ViewHolder(View itemView) {
            super(itemView);
            mArrivalTime = (TextView) itemView.findViewById(R.id.tvArrivalTime);
            mPlatform = (TextView) itemView.findViewById(R.id.tvPlatform);
            mTime = (TextView) itemView.findViewById(R.id.tvTime);
            mStatus = (TextView) itemView.findViewById(R.id.tvStatus);
            mDestinationTime = (TextView) itemView.findViewById(R.id.tvDestinationTime);
            mDestination = (TextView) itemView.findViewById(R.id.tvDestination);
        }
    }
}

2 个答案:

答案 0 :(得分:2)

您可以使用tag

position查看您的观点
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
    ...
    holder.someClickableElement.setTag(position);
    ...
}

然后在ArrivalTimeOnClickListener

public void ArrivalTimeOnClickListener(View view) {
    int position = (int) view.getTag();
    adapter.mTrains.get(position); // make mTrains public first, or provide getter
    ...
}

答案 1 :(得分:1)

您可以为点击处理创建自定义界面。

看看我的例子

public interface RecyclerViewClickListener{
    void onItemClick(View view, int position);
}

public class TrainAdapter extends RecyclerView.Adapter<TrainAdapter.ViewHolder> {
    private ArrayList<Train> mTrains;
    private RecyclerViewClickListener mListener;

    public TrainAdapter(Context context, ArrayList<Train> trains, RecyclerViewClickListener listener){
        mTrains = trains;
        mListener = listener;
    }

...

public ViewHolder(final View itemView) {
        super(itemView);
        mArrivalTime = (TextView) itemView.findViewById(R.id.tvArrivalTime);
        mPlatform = (TextView) itemView.findViewById(R.id.tvPlatform);
        mTime = (TextView) itemView.findViewById(R.id.tvTime);
        mStatus = (TextView) itemView.findViewById(R.id.tvStatus);
        mDestinationTime = (TextView) itemView.findViewById(R.id.tvDestinationTime);
        mDestination = (TextView) itemView.findViewById(R.id.tvDestination);
        itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mListener.onItemClick(itemView, getAdapterPosition());
            }
        });

.....

课堂上的

trains.add(new Train("Artarmion Platform 3", 7, "On time", "Ahfield", "15:01"));
trains.add(new Train("TEST", 97, "TEST", "TEST", "TEST"));

trainAdapter = new TrainAdapter(this, trains, new RecyclerViewClickListener(){
   @Overide
   void onItemClick(View view, int position){
       ArrivalTimeOnClickListener(view,position);
  }
});