我正在尝试使ImageCardView
在聚焦后扩大尺寸,如所示的here我为Presenter
创建了ImageCardView
并添加了{{1 }}中显示的XML中,在cardview上添加了焦点侦听器,并在focus上调用了ListRowView
,但是我仍然无法使其扩展。我在这里想念什么?
下面的代码
ImageCardView.setSelected(b)
activity_main.xml
<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="match_parent"
tools:context=".MainActivity">
<androidx.leanback.widget.ListRowView
android:id="@+id/listRowView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#FF0000"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
main_activity.java
public class MainActivity extends FragmentActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListRowView listRowView = findViewById(R.id.listRowView);
ArrayObjectAdapter adapter = new ArrayObjectAdapter(new TestPresenter());
for(int i = 0; i < 5; i++){
Movie movie = new Movie("Test " + i);
adapter.add(movie);
}
ItemBridgeAdapter bridgeAdapter = new ItemBridgeAdapter(adapter);
listRowView.getGridView().setAdapter(bridgeAdapter);
}
}
TestPresenter.java
public class TestPresenter extends Presenter {
private final static String TAG = TestPresenter.class.getSimpleName();
private static Context mContext;
private static int sSelectedBackgroundColor;
private static int sDefaultBackgroundColor;
private static void updateCardBackgroundColor(ImageCardView view, boolean selected) {
int color = selected ? sSelectedBackgroundColor : sDefaultBackgroundColor;
view.setBackgroundColor(color);
view.findViewById(R.id.info_field).setBackgroundColor(color);
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent) {
mContext = parent.getContext();
sDefaultBackgroundColor = parent.getResources().getColor(R.color.lb_default_brand_color, null);
sSelectedBackgroundColor = parent.getResources().getColor(R.color.lb_tv_white, null);
final ImageCardView cardView = new ImageCardView(mContext);
cardView.setFocusable(true);
cardView.setFocusableInTouchMode(true);
cardView.setClickable(true);
cardView.setCardType(BaseCardView.CARD_TYPE_INFO_UNDER_WITH_EXTRA);
cardView.setInfoVisibility(BaseCardView.CARD_REGION_VISIBLE_ALWAYS);
cardView.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View view, final boolean b) {
updateCardBackgroundColor(cardView, b);
cardView.setSelected(b);
}
});
((TextView)cardView.findViewById(R.id.title_text)).setTextColor(mContext.getColor(R.color.lb_tv_white));
return new ViewHolder(cardView);
}
@Override
public void onBindViewHolder(Presenter.ViewHolder viewHolder, Object item) {
Movie movie = (Movie)item;
((ViewHolder) viewHolder).mCardView.setTitleText(movie.getTitle());
((ViewHolder) viewHolder).updateCardViewImage(movie.getUri());
}
@Override
public void onUnbindViewHolder(Presenter.ViewHolder viewHolder) {
}
static class ViewHolder extends Presenter.ViewHolder {
private ImageCardView mCardView;
private Drawable mDefaultCardImage;
private PicassoImageCardViewTarget mImageCardViewTarget;
ViewHolder(View view) {
super(view);
mCardView = (ImageCardView)view;
mImageCardViewTarget = new PicassoImageCardViewTarget(mCardView);
mDefaultCardImage = mContext.getResources().getDrawable(R.drawable.placeholder, null);
}
void updateCardViewImage(Uri uri){
/* */
}
}
public static class PicassoImageCardViewTarget implements Target {
/* */
}
}
Movie.java