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

时间:2018-09-09 10:54:55

标签: android android-recyclerview

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

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

有人可以帮我吗?

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>

...

屏幕截图:

want

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

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

提前谢谢!

2 个答案:

答案 0 :(得分:1)

在适配器类中:

@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 :(得分:0)

首先使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);
}