Ich baue ein App-Schublade-Fragment und verwende eine GridView zur Anzeige jedes App-Elements. Derzeit scrollt die Schublade nicht vertikal, wie ich es mir erhofft hatte. Ich würde es vorziehen, die ImageButtons als einzige fokussierbare Elemente zu verwenden, um mit meinen anderen Bildschirmen konsistent zu bleiben. Im Wesentlichen erfolgt die gesamte Navigation durch Verschieben zwischen den ImageButtons (nicht dem GridView-Bereich, der jedem Element zugeordnet ist). Diese App ist ein Launcher für ein Fernsehgerät, daher sollte die GridView mit Remote-Eingabe scrollen. Die Schaltflächen sind jetzt navigierbar, aber nur die tatsächlich angezeigten.Android GridView Scrollen mit ImageButtons
Ich stecke fest, um zu versuchen, die Scrolling-Funktionalität zu arbeiten. Ich nehme an, es hat damit zu tun, Fokus zu den ImageButtons zu zwingen, aber ich bin unsicher, wie man das behebt. Jede Hilfe wird geschätzt!
fragment_app_drawer.xml
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="ninjabox.tv.com.ninjabox.AppDrawerFragment"
android:background="@color/dark_gray">
<GridView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/appsGridLayout"
android:numColumns="5"
android:verticalSpacing="10dp"
android:columnWidth="50dp"
android:stretchMode="columnWidth"
android:gravity="center"
android:focusable="false"
android:descendantFocusability="afterDescendants"
android:scrollIndicators="right">
</GridView>
<ProgressBar
style="?android:attr/progressBarStyleLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/fragmentProgressBar"
android:layout_gravity="center"
android:visibility="invisible"/>
</FrameLayout>
app_drawer_item.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="25dp"
android:clipToPadding="false"
android:focusable="false">
<ImageButton
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/appIcon"
android:scaleType="fitCenter"
android:adjustViewBounds="true"
android:cropToPadding="false"
android:stateListAnimator="@anim/button_elevation"
android:background="@drawable/image_button_drawable"
android:elevation="@dimen/elevation_default"
android:src="@drawable/spotify_icon"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:padding="8dp"
android:focusable="true"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/appLabel"
android:textSize="@dimen/abc_text_size_medium_material"
android:textColor="#ffffff"
android:paddingTop="8dp"
android:paddingBottom="8dp"
android:text="App Name"
android:layout_below="@+id/appIcon"
android:layout_centerHorizontal="true"/>
</RelativeLayout>
AppDrawerFragment.java ArrayAdapter
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
ArrayAdapter<AppDetail> adapter = new ArrayAdapter<AppDetail>(
getActivity(),
R.layout.app_drawer_item,
MainActivity.getApps()) {
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
if(convertView == null){
convertView = getLayoutInflater(null).inflate(R.layout.app_drawer_item, null);
}
ImageButton appIconButton = (ImageButton)convertView.findViewById(R.id.appIcon);
appIconButton.setImageDrawable(mApps.get(position).getIcon());
if (position == 0) {
appIconButton.requestFocus();
}
appIconButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getContext(), mApps.get(position).getName(), Toast.LENGTH_LONG).show();
}
});
TextView appLabel = (TextView)convertView.findViewById(R.id.appLabel);
appLabel.setText(mApps.get(position).getLabel());
return convertView;
}
};
mGridView.setAdapter(adapter);
}
Am Ende habe ich nur meine ImageButtons zu ImageViews getauscht und einen Selektor für die GridView –