I.はじめに
最近RecyclerViewのセカンダリリストの使い方を勉強しているのですが、実現したい効果は以下の通りです。
そこでいくつかのブログをチェックしたところ、実装方法が複雑すぎると感じ、特にこの方法は推奨されるものではなかったので、他の人にアドバイスを求めて、良い感じの実装方法を入手しました。実装のアイデアは、ページ全体が ScrollView であり、ScrollView 内の LinearLayout の高さが Wrap_content に設定され、Layout インターフェイスが動的に追加されます (このインターフェイスのコンテンツはタイトルと RecyclerView です)。
2. 例
上記の解決策が可能であることを示すために、簡単なデモを実装しました。このデモでは、Layout インターフェイスを動的に追加でき、追加された Layout インターフェイスを削除することもできます。
アクティビティのコードは以下の通りです。次のコードでは、主にグローバル変数 new_view を設定して、新しい Layout インターフェイスを作成するときにそれを記録し、削除したいときにすぐに削除できるようにしています。Layout インターフェイスの追加は、コンテナー (LinearLayout、RelativeLayout、または FrameLayout など) の addView(View view) メソッドを通じて実装され、Layout インターフェイスの削除は、コンテナーのremoveView(View view) メソッドを通じて実装されます。
package com.cs.blackbox;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
public class DynamicAddAndDelActivity extends AppCompatActivity {
View new_view = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dynamic_add_del);
LinearLayout ll = findViewById(R.id.dad_ll_main);
Button bt_add = findViewById(R.id.dad_bt_add);
Button bt_delete = findViewById(R.id.dad_bt_delete);
LayoutInflater li = LayoutInflater.from(this);
bt_add.setOnClickListener(v -> {
new_view = li.inflate(R.layout.item_demo, null, false);
addItemView(ll, new_view);
});
bt_delete.setOnClickListener(v -> {
removeItemView(ll, new_view);
new_view = null;
});
}
private void addItemView(LinearLayout ll, View view) {
if (view != null) {
ll.addView(view);
}
}
private void removeItemView(LinearLayout ll, View view) {
if (view != null) {
ll.removeView(view);
}
}
}
インターフェイスは次のとおりで、非常にシンプルです
対応するxmlコードは以下のとおりです。主なことは、LinearLayout (方向は垂直) をコンテナとして設定することです。この場合、レイアウトを追加する際は最後に追加されます。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/dad_ll_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".DynamicAddAndDelActivity">
<Button
android:id="@+id/dad_bt_add"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="add" />
<Button
android:id="@+id/dad_bt_delete"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="delete" />
</LinearLayout>
追加したいレイアウトインターフェイスは次のとおりです
対応するXMLコードは次のとおりです
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:orientation="horizontal">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:text="扫描" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_weight="1"
android:text="解析" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="horizontal">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:visibility="visible">
<ImageView
android:layout_width="120dp"
android:layout_height="120dp"
android:layout_gravity="center"
android:background="@android:drawable/btn_star_big_on" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="图片"
android:textSize="20sp" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
3. 効果
最初の図はデフォルトのビュー、2 番目の図は「追加」をクリックしたとき、3 番目の図は「削除」をクリックしたときです。
4. まとめ
上記のコードには RecyclerView の内容はありませんが、特定のコントロールをこれに置き換えても同じです。最後の追加は LinearLayout の addView(View view) メソッドによって行われ、削除も LinearLayout の RemoveView(ビュービュー)。したがって、インターフェイス全体でレイアウトやコントロールを動的に追加または削除する必要がある場合は、グローバル変数を設定する必要があります。RecyclerView のセカンダリ リストは、リスト データ構造を通じて保存および管理できます。