Recyclerview 所选项目的背景图像在较小的屏幕设备上太大
Recyclerview selected item's background image too large on smaller screen devices
我有一个 RecyclerView
有 CardView
来显示项目。这些项目包含图片浏览量和 TextView
一旦用户 select 有一个项目,它就会被填充。一旦他们 select 一个项目,我检索 selected
项目的背景并激发打开新 activity 的意图,我在其中动态创建布局并
将 selected 项目的背景设置为布局的背景。这是我的适配器 class
填充我的 RecyclerView
我检索 selected 项目的背景。
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();
}
}
在下一个 activity 中,我按如下方式检索 selected 项目。
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 英寸的设备上测试它时,我看到非常大的背景(我可以永远滚动),我只能看到两个图像,但看不到文本视图。在较大的设备(包括真实设备和模拟器)中,我得到了正确尺寸的背景。但我看不到像较小的文本视图
设备。不过我看到了这两张图片。
<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>-->
我似乎无法弄清楚为什么会发生这种情况以及如何解决它。感谢任何帮助。
像
一样创建文件夹
Mobile : 创建不同值的文件夹,如下所述:
values (For mdpi devices)
values-hdpi (For hdpi devices)
values-xhdpi (For xhdpi devices)
values-xxhdpi (For xxhdpi devices)
平板电脑:创建不同的布局文件夹,如下所述:
layout-sw600dp (For 7? to 9? Screen)
layout-sw720dp (For 10? to above screen)
图片资源:创建4个drawable文件夹:
drawable-mdpi
drawable-hdpi
drawable-xhdpi
drawable-xxhdpi
如果您通过 api 处理它。使用不同的图像大小
imagesmall_link
imagelarge_link
imagexlarge_link
我通过简单地将布局高度从 wrap_content 更改为 300dp 之类的有限尺寸来解决尺寸问题,这似乎适用于所有屏幕尺寸。但是文本视图在所有屏幕上都不可见,这很奇怪。当我解决这个问题时,我会更新这个答案,这样它有时可能会对某人有所帮助。
我有一个 RecyclerView
有 CardView
来显示项目。这些项目包含图片浏览量和 TextView
一旦用户 select 有一个项目,它就会被填充。一旦他们 select 一个项目,我检索 selected
项目的背景并激发打开新 activity 的意图,我在其中动态创建布局并
将 selected 项目的背景设置为布局的背景。这是我的适配器 class
填充我的 RecyclerView
我检索 selected 项目的背景。
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();
}
}
在下一个 activity 中,我按如下方式检索 selected 项目。
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 英寸的设备上测试它时,我看到非常大的背景(我可以永远滚动),我只能看到两个图像,但看不到文本视图。在较大的设备(包括真实设备和模拟器)中,我得到了正确尺寸的背景。但我看不到像较小的文本视图 设备。不过我看到了这两张图片。
<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>-->
我似乎无法弄清楚为什么会发生这种情况以及如何解决它。感谢任何帮助。
像
一样创建文件夹Mobile : 创建不同值的文件夹,如下所述:
values (For mdpi devices)
values-hdpi (For hdpi devices)
values-xhdpi (For xhdpi devices)
values-xxhdpi (For xxhdpi devices)
平板电脑:创建不同的布局文件夹,如下所述:
layout-sw600dp (For 7? to 9? Screen)
layout-sw720dp (For 10? to above screen)
图片资源:创建4个drawable文件夹:
drawable-mdpi
drawable-hdpi
drawable-xhdpi
drawable-xxhdpi
如果您通过 api 处理它。使用不同的图像大小 imagesmall_link imagelarge_link imagexlarge_link
我通过简单地将布局高度从 wrap_content 更改为 300dp 之类的有限尺寸来解决尺寸问题,这似乎适用于所有屏幕尺寸。但是文本视图在所有屏幕上都不可见,这很奇怪。当我解决这个问题时,我会更新这个答案,这样它有时可能会对某人有所帮助。