私は2つのレイアウトファイル(activity_test.xml)及び(content.xml)と1つのJavaファイル(ActivityTest.java)を持っています
示すようActivityTest.javaは(activity_test.xml)からのコンテンツのレイアウトを設定します
ActivityTest.java
@Override
protected void onCreate(Bundle savedInstanceState) {
.....................
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
}
示されているようactivity_test.xmlはrecyclerViewが含まれています
activity_test.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
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:layout_width="match_parent"
android:layout_height="wrap_content"
android:fitsSystemWindows="true"
tools:context=".ActivityTest">
............
some views
...........
<androidx.recyclerview.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/recycler_view2"
android:layout_marginTop="30dp"
android:layout_alignParentBottom="true"
android:scrollbars="vertical"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0"
/>
Iは、図示のように(content.xml)レイアウトを膨張RecyclerViewAdapterを有します
RecyclerViewAdapter
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.MyViewHolder> {
.............
@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.content, parent, false);
return new RecyclerViewAdapter.MyViewHolder(itemView);
}
}
ここでcontent.xmlは、
content.xml
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:context=".TestActivity">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
...........
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/save"
android:src="@drawable/outline_share_black_48dp"
android:layout_toLeftOf="@+id/savePhoto"
android:layout_marginTop="5dp"
android:layout_marginRight="20dp"
android:clickable="true"
android:onClick="saveIntent"
/>
問題 - idを持つ上記のImageViewの上でクリックしてリスナーを設定するには、(保存)というエラーがスローされます。
OnClickListener、OnTouchLlistener、setVisibility全てがエラーをスローする- -などClickEvents 「ヌルオブジェクト参照に仮想メソッド『ボイドandroid.widget.ImageView.setVisibility(INT)』を呼び出そうと」
たとえば、
MainActivity.java
..........
private ImageView save_icon;
..........
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
.............
save_icon = findViewById(R.id.save);
........
public void saveIntent(View view){
if(view.getId() == R.id.savePhotoIcon){
save_icon.setImageResource(R.drawable.outline_bookmark_black_48dp);
}
}
私は別の画像にImageViewの画像のsrcを変更したいのonClick
それでも同じエラーを投げる(::クリッカブルとアンドロイドのOnClickアンドロイド) - 私は、XMLせずにこれをやってみました。
それはMainActivity.Javaファイル内のonCreateでsetContentViewレイアウトとして使用wasnt'tので、ほとんどの場合content.xml中(保存)IDはNULL値を返しています。
私は(activity_test.xml)からのビューに同じ事をしようとしたとき、それは動作します。
けれども、などのアクション - トーストメッセージ、新しいテントのすべての作品を表示します。例えば
public void saveIntent(View view){
Intent intent = new Intent(ActivityTest.this, AnotherActivity.class);
startActivity(intent);
}
あなたはレイアウトテンプレートを使用して、実行時にビューの所有者を作成している、とあなたは、画面上のビューショーの前にビューホルダーを呼び出すためです。このために、あなたはonBindViewHolderでそれを実装する必要があります。
public void onBindViewHolder(MyViewHolder holder, int position)
{
ImageView imgView=holder.itemview.findViewById(R.id.save);
imgView.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Code here executes when user click on save
}
});
}