0

アイテムを表示する必要がRecyclerViewあります。CardViewアイテムには画像ビューが含まれてTextView おり、ユーザーがアイテムを選択すると、画像が表示されます。アイテムを選択したら、選択したアイテムの背景を取得し、インテントを起動して新しいアクティビティを開きます。このアクティビティでは、動的にレイアウトを作成し、選択したアイテムの背景をレイアウトの背景として設定します。RecyclerViewこれは、選択した項目の背景を取得する場所にデータを入力するアダプター クラスです。

public class PersonsAdapter extends RecyclerView.Adapter<PersonsAdapter.MyViewHolder> {

        private List<Person> mpersonsList;


        public class MyViewHolder extends RecyclerView.ViewHolder {
            public TextView name .....;
            View v;
            Person p;

            public MyViewHolder(View view) {
                super(view);
                name = (TextView) view.findViewById(R.id.tvName);
                //and all other textviews....

                v = view;
                v.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        //if (view.getId() == getAdapterPosition()) {
                            //int itemposition = mRecyclerView.indexOfChild(v);
                            Intent i = new Intent(context, SelectedTemplate.class);
                            startActivity(i);
                            theBackground = view.getBackground();//retrieve the background of the selected item
                        //}
                    }
                });
                List<Drawable> templatesList = new ArrayList<>();
            }
        }


        public PersonsAdapter(List<Person> personsList) {
            this.mpersonsList = personsList;
        }

        @Override
        public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.person, parent, false);

            return new MyViewHolder(itemView);
        }

        @Override
        public void onBindViewHolder(MyViewHolder holder, int position) {
            Person person = mpersonsList.get(position);
            holder.name.setText(person.getName());
            //and other details of the item......

            holder.p = mpersonsList.get(position);

            if(position == 0){
                holder.v.setBackgroundResource(R.drawable.wavy_curves_onblack);
            } 
        //and other background images.....
        }

        @Override
        public int getItemCount() {
            return mpersonsList.size();
        }
    }

次のアクティビティでは、選択した項目を次のように取得します。

private void getSelectedTemplate() {
        LayoutInflater vi = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View v = vi.inflate(R.layout.selected_template, null);

        // fill in any details dynamically here


        //insert into main view
        ViewGroup insertPoint = (ViewGroup) findViewById(R.id.selectedtemplatelayout);
        insertPoint.setBackground(TheTemplates.theBackground);
        insertPoint.addView(v, 0, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));

        rl = (LinearLayout) findViewById(R.id.selectedtemplatelayout);
        image.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));
        image.getLayoutParams().height = (int) getResources().getDimension(R.dimen.image_height);
        image.getLayoutParams().width = (int) getResources().getDimension(R.dimen.image_width);



        name.setText("NAME");
        name.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));
        //add all other textviews dynamically.....

        rl.removeAllViews();
        rl.addView(image);
        rl.addView(name);
        //add all remaining textviews
    }

エミュレーターまたは 5.5 インチ未満のデバイスでテストすると、非常に大きな背景が表示され (永遠にスクロールできます)、2 つの画像しか表示されず、テキストビューは表示されません。より大きなデバイス(リアルとエミュレーターの両方)では、適切なサイズの背景が得られます。しかし、小さいデバイスのようにテキストビューが表示されません。しかし、私は2つの画像を見ます。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:focusable="true"
    android:focusableInTouchMode="true"><!--this two lines will hide the keyboard and make the NAME textview visible -->
    <android.support.v7.widget.Toolbar
        android:id="@+id/my_toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>
    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

    <LinearLayout
        android:id="@+id/selectedtemplatelayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="4dp"
        android:orientation="vertical" >
//this is the layout I would like to stuff all my textviews and images and //set the background of the selected item.
    </LinearLayout>

    </ScrollView>


//another layout
    <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fillViewport="true">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">
        </LinearLayout>
    </ScrollView>
</LinearLayout>

これはRecyclerView

<?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"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">-->

    <android.support.v7.widget.RecyclerView
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/templateList"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="center_horizontal"
        />
<!--</LinearLayout>-->

なぜこれが起こっているのか、どうすれば修正できるのかわかりません。どんな助けでも大歓迎です。

4

2 に答える 2

0

レイアウトの高さを wrap_content から 300dp などの制限されたサイズに変更するだけでサイズの問題を解決しました。これはすべての画面サイズで機能するようです。しかし、奇妙なことに、すべての画面でテキストビューが表示されるわけではありません。修正したらこの回答を更新するので、いつか誰かを助けるかもしれません。

于 2016-10-18T18:50:34.607 に答える