Ich musste eine horizontale Listenansicht erstellen, nach der Suche fand ich eine Lösung, die TwoWayView ist.Erstellen Sie einen benutzerdefinierten Adapter für TwoWayView
Die „Textview“ example auf dem Internet funktioniert für mich zu finden, aber ich brauche es nicht ein Image Artikel Textview so habe ich einen Adapter:
public class Custom_Supermarket_Adapter_2 extends ArrayAdapter<Integer> {
private Context context;
private Integer[] imagelist;
private LayoutInflater mInflater;
private View view;
private DatabaseHandler dbhandler;
private ImageView imageview;
private int resource;
public Custom_Supermarket_Adapter_2(Context context, int resource, Integer[] objects) {
super(context, resource, objects);
this.context = context;
this.imagelist = objects;
this.resource = resource;
mInflater = LayoutInflater.from(context);
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
if (convertView == null) {
view = mInflater.inflate(resource, null);
}else{
view = convertView;
}
imageview = (ImageView)view.findViewById(R.id.customimageview);
imageview.setImageResource(imagelist[position]);
imageview.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.v("POSITION CLICKED",Integer.toString(position));
}
});
return view;
}
}
Und ich nenne es in der Klasse wie folgt:
/*These are the array of the image I want to be displayed*/
private Integer[] mThumbIds = {
R.drawable.citimart,R.drawable.coopmart,R.drawable.lottemart,R.drawable.maximark,R.drawable.metro
};
Custom_Supermarket_Adapter_2 aItems = new Custom_Supermarket_Adapter_2(this,R.layout.simple_list_item, mThumbIds);
TwoWayView lvTest = (TwoWayView) findViewById(R.id.lvItems);
lvTest.setAdapter(aItems);
Das ist mein simple_list_item
xML-Datei
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/customimageview"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:textAppearance="?android:attr/textAppearanceListItemSmall"
android:gravity="center_vertical" />
Und dies ist das Haupt Layout, das die horizontale Listenansicht enthält:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<org.lucasr.twowayview.TwoWayView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/lvItems"
style="@style/TwoWayView"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="2"
android:drawSelectorOnTop="false"
tools:context=".Listing_items"
android:background="@android:color/holo_purple"/>
<GridView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="10"
android:id="@+id/gridView_main"
android:stretchMode="columnWidth"
android:numColumns="2"
android:horizontalSpacing="1dp"
android:verticalSpacing="1dp"/>
</LinearLayout>
Wenn ich die app laufen, ich sehe nur die leeren TwoWayView
, kein Bild angezeigt wird, habe ich versucht, innerhalb dieser TwoWayView überall tippen Sie auf, The logcat zeigt "POSITION CLICKED 0" an, tippte überall und nur auf Position 0. Es sieht so aus, als ob nur das erste Bild bearbeitet wird, aber irgendwie kann es nicht gerendert werden. Fehle ich eine Schleife oder etwas, um es alle Bilder zeigen zu lassen? Und habe ich findviewbyID
die falsche?
Vielen Dank für Ihre Zeit.
Vielen Dank für Ihre Antwort, ich fand was ich vermisst habe. –