繁体   English   中英

通过单击回收者视图中的项目来更新活动中的视图

[英]Update views in activity by clicking item in recycler view

我有一个回收站视图,其中填充有信息列表。

当我单击一个项目时,我希望它填充活动中的字段。 我不确定如何从适配器执行此操作。

有人能帮助我吗?

MainActivity.java

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    b1 = (Button) findViewById(R.id.btnadd);
    b1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent i = new Intent(getApplicationContext(), AddActivity.class);
            startActivity(i);
        }
    });
    recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
    mAdapter = new MovieAdapter(getApplicationContext(), movieList);
    RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext());
    recyclerView.setLayoutManager(mLayoutManager);
    recyclerView.setItemAnimator(new DefaultItemAnimator());
    recyclerView.setAdapter(mAdapter);
    prepareMovieData();

}

适配器类别:

@Override
public void onBindViewHolder(MyViewHolder holder, final int position) {
    final Movies movie = moviesList.get(position);
    holder.title.setText(movie.getTitle());
    holder.genre.setText(movie.getGenre());
    holder.year.setText(String.valueOf(movie.getYear()));
    if (selected_position == position) {
        holder.itemView.setBackgroundColor(Color.LTGRAY);
    } else {
        holder.itemView.setBackgroundColor(Color.WHITE);
    }

    holder.itemView.setOnClickListener(v -> {
        //if (position == RecyclerView.NO_POSITION) return;
        notifyItemChanged(selected_position);
        selected_position = position;
        notifyItemChanged(selected_position);
    });
}

我要填写的视图activity_main.xml

...

<LinearLayout
    android:id="@+id/llname"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <TextView
        android:id="@+id/txtmov"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Movie" />

    <EditText
        android:id="@+id/etmov"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

</LinearLayout>

<LinearLayout
    android:id="@+id/llgenre"
    android:layout_below="@+id/llname"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    >

    <TextView
        android:id="@+id/txtgnr"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Genre" />

    <EditText
        android:id="@+id/etgnr"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

</LinearLayout>

<LinearLayout
    android:id="@+id/llyear"
    android:layout_below="@+id/llgenre"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <TextView
        android:id="@+id/txtyr"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Year " />

    <EditText
        android:id="@+id/etyr"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="number" />

</LinearLayout>

...

屏幕截图:

想

我想到了不同的方法,一种方法是我自己创建一个适配器,然后在mainactivity中构造它,就像使用我制作的movieAdapter一样。 我尝试了一下,但是没有用,然后我想到自己必须有一种更简单的方法来做,但是我正在努力寻找任何东西。

堆栈也不会让我发帖,因为它说代码太多,所以如果您需要其他任何内容,就给我喊一声。 我离开了我认为重要的部分。

先感谢您!

在适配器类中:

@Override
public void onBindViewHolder(MyViewHolder holder, final int position) {
    final Movies movie = moviesList.get(position);

    ...

    holder.itemView.setTag(movie)

    holder.itemView.setOnClickListener(v -> {

        ...

        MainActivity.updateView((Movies)v.getTag())
    });
}

MainActivity.java

public static void updateView(Movies movies){

    // Update view by using `findViewById`
}

编辑

这个例子应该可以帮助您解决

错误:无法从静态上下文引用非静态方法findViewById(int)

MainActivity.java

public class MainActivity extends AppCompatActivity {

    private static LinearLayout linearLayout = null;

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

        linearLayout = findViewById(R.id.root_view);
        updateView("Hello World! 2");
    }

    public static void updateView(String text){
        if(linearLayout==null) return;
        ((TextView) linearLayout.findViewById(R.id.tv)).setText(text);
    }
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/root_view"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</LinearLayout>

你可以这样做 :

1-首先创建一个这样的界面

public interface OnMovieClickListener {

      void onClick(int movieId);

}

2-第二,您的活动应实现此接口

3-在您的ViewHolder构造函数中,您应该执行以下操作:

ViewHolder(View itemView) {
      itemView.setOnClickListener(new OnClickListener() {
              public void onClick() {
                      listener.onClick(this.getLayoutPosition());
              }
      }
}

您的适配器的构造函数是这样的:

private OnMovieClickListener listener;
public adapter(OnMovieClickListener listener) {
         this.listener = listener
}

请注意,您的Viewholder内部类不应为静态。 最后将您的活动传递给适配器;

Adapter adapter = new Adapter(this);

首先使Movie类可拆分

onBindViewHolder

    holder.itemView.setTag(movie);
    holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Movie movie = (movie) view.getTag();

                Intent intent = new Intent(MainActivity.BROADCAST_ACTION);
                intent.putExtra("ITEM_DATA", movie);
                LocalBroadcastManager.getInstance(context).sendBroadcast(intent);
            }
        });

在您的activity

public static final String BROADCAST_ACTION = "BROADCAST_ACTION";

BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent.getAction() != null &&
                intent.getAction().equals(BROADCAST_ACTION)) {
            Bundle bundle = intent.getExtras();
            if (bundle!=null){
                Movie movie = bundle.getParcelable("ITEM_DATA");
                //Use movie for setting data to your layout
            }
        }
    }
};


@Override
protected void onResume() {
    super.onResume();
    LocalBroadcastManager.getInstance(
            MainActivity.this).registerReceiver(broadcastReceiver, new IntentFilter(BROADCAST_ACTION));
}

@Override
protected void onPause() {
    super.onPause();
    LocalBroadcastManager.getInstance(MainActivity.this).unregisterReceiver(broadcastReceiver);
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM