我从数据库获取数据,将其保存在列表中,然后将列表传递给自定义的recyclerView适配器
问题是滚动后,一些单词会被截断:为什么?我怎么能够
解决那个?
检查第二张图片中的charmander和charmeeleon,如果您需要代码,请问您需要什么
编辑:
适配器:
public class PokedexAdapter extends RecyclerView.Adapter<PokedexAdapter.ViewHolder> {
private List<PokemonBase> mData;
private Context context;
private MainActivity mNavigationDrawerCallbacks;
private View mSelectedView;
private int mSelectedPosition;
public PokedexAdapter(List<PokemonBase> data, Context xontext) {
mData = data;
context = xontext;
}
public void setFumettiCallbacks(MainActivity navigationDrawerCallbacks) {
mNavigationDrawerCallbacks = navigationDrawerCallbacks;
}
@Override
public PokedexAdapter.ViewHolder onCreateViewHolder(final ViewGroup viewGroup, int i) {
View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.element, viewGroup, false);
final ViewHolder viewHolder = new ViewHolder(v);
viewHolder.itemView.setClickable(true);
viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(context, PokemonActivity.class);
intent.putExtra("poke", mData.get(viewHolder.getAdapterPosition()).numero);
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
Pair<View, String> p1 = Pair.create( (View)viewHolder.img, "profile");
ActivityOptionsCompat options = ActivityOptionsCompat.
makeSceneTransitionAnimation((MainActivity)context,(View)viewHolder.img, "profile");
context.startActivity(intent,options.toBundle());}
else
context.startActivity(intent);
}
});
return viewHolder;
}
@Override
public void onBindViewHolder(PokedexAdapter.ViewHolder viewHolder, int i) { //here i set the data
viewHolder.nome.setText(getStringIdentifier(context, mData.get(i).nome));
viewHolder.tipouno.setText(getStringIdentifier(context, mData.get(i).tipo));
viewHolder.tipouno.setTextColor(context.getResources().getColor(getColorIdentifier(context, mData.get(i).tipo)));
if(mData.get(i).tipo2 != null){viewHolder.tipodue.setText(getStringIdentifier(context, mData.get(i).tipo2));
viewHolder.tipodue.setTextColor(context.getResources().getColor(getColorIdentifier(context, mData.get(i).tipo2)));}
else viewHolder.tipodue.setText("");
viewHolder.numero.setText(String.valueOf(mData.get(i).numero));
viewHolder.img.setImageResource(getImageIdentifier(context, mData.get(i).immagine));
}
public void selectPosition(int position) {
mSelectedPosition = position;
notifyItemChanged(position);
}
@Override
public int getItemCount() {
return mData != null ? mData.size() : 0;
}
public static class ViewHolder extends RecyclerView.ViewHolder {
public TextView nome;
public TextView tipouno;
public TextView tipodue;
public TextView numero;
public ImageView img;
public ViewHolder(View itemView) {
super(itemView);
nome = (TextView) itemView.findViewById(R.id.nome);
tipouno = (TextView) itemView.findViewById(R.id.tipoprimo);
tipodue = (TextView) itemView.findViewById(R.id.tiposecondo);
numero = (TextView) itemView.findViewById(R.id.numero);
img = (ImageView) itemView.findViewById(R.id.immagine);
}
}
public static int getStringIdentifier(Context context, String name) {
return context.getResources().getIdentifier(name, "string", context.getPackageName());
}
public static int getImageIdentifier(Context context, String name) {
return context.getResources().getIdentifier(name, "drawable", context.getPackageName());
}
public static int getColorIdentifier(Context context, String name) {
return context.getResources().getIdentifier(name, "color", context.getPackageName());
}
}
PokemonBase类:
public class PokemonBase {
int numero;
String nome;
String tipo;
String tipo2;
String immagine;
PokemonBase(){
}
}
element.xml:
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="72dp"
xmlns:android="http://schemas.android.com/apk/res/android">
<ImageView
android:layout_width="40dp"
android:layout_height="wrap_content"
android:id="@+id/immagine"
android:maxHeight="40dp"
android:maxWidth="40dp"
android:minHeight="40dp"
android:minWidth="40dp"
android:layout_marginTop="16dp"
android:layout_marginBottom="16dp"
android:layout_marginLeft="16dp"
android:transitionName="profile"
android:scaleType="centerCrop"/>
<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_marginLeft="16dp" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:id="@+id/nome"
android:layout_marginTop="5dp"
android:transitionName="name"
android:scaleType="centerCrop"/>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="fill_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:textAppearance="?android:attr/textAppearanceMedium"
android:id="@+id/tipoprimo" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" "
android:id="@+id/textView2" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:id="@+id/tiposecondo" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_vertical|right">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:id="@+id/numero"
android:layout_marginRight="16dp" />
</LinearLayout>
</LinearLayout>
最佳答案
将您的布局更改为如下所示
<LinearLayout>
<ImageView/>
<LinearLayout
android:orientation="vertical"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="fill_parent"
android:layout_marginLeft="16dp" >
...
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center_vertical|right">
...
</LinearLayout>
</LinearLayout>
仅更改了
layout_weight
和layout_width
,我跳过了不需要更改的部分。