Мне нужно было сделать горизонтальный список, после поиска я нашел решение, которое является TwoWayView.Создайте пользовательский адаптер для TwoWayView
«TextView» example в интернете работает найти для меня, но мне нужно это пункт ImageView не TextView поэтому я создал адаптер:
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;
}
}
И я называю это в классе, как это:
/*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);
Это мой simple_list_item
XML файл
<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" />
И Тхи s является основным макетом, который содержит горизонтальную ListView:
<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>
Когда я запустить приложение, я вижу только пустой TwoWayView
, не отображается изображения, я пытался нажать на везде внутри этого TwoWayView, The LogCat шоу " POSITION CLICKED 0 ", постукиваемое повсюду и только позиция 0. Похоже, что обрабатывается только первое изображение, но каким-то образом оно не может быть отображено. Мне не хватает петли или что-то, чтобы показать все изображения? И сделал ли я findviewbyID
неправильный?
Спасибо за ваше время.
Спасибо за ваш ответ, я нашел то, что пропустил. –