我创建了一个Android应用程序,我希望在ListView中垂直显示一些图像数据。
如何使它成为可能?
我尝试使用此代码 -
<LinearLayout
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="360dp"
android:layout_above="@+id/relativeLayout2"
android:layout_below="@+id/relativeLayout1"
android:gravity="center"
android:orientation="vertical" >
<ListView
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="left|top"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="15dp"
app:layout_gravity="fill_vertical"
android:entries="@array/country_image_list" >
</ListView>
</LinearLayout>
答案 0 :(得分:0)
public class ItemImagesAdapter extends BaseAdapter {
private Context context;
private ArrayList<String> paths;
private ImageLoader iml;
public ItemImagesAdapter(FragmentActivity activity, ArrayList<String> image_paths) {
context = activity;
paths = image_paths;
ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(activity).build();
ImageLoader.getInstance().init(config);
iml = ImageLoader.getInstance();
}
@Override
public int getCount() {
return paths.size();
}
@Override
public Object getItem(int position) {
return paths.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = LayoutInflater.from(context);
if (convertView == null) {
convertView = inflater.inflate(R.layout.list_item_image, parent, false);
}
String path = paths.get(position);
ImageView image = (ImageView) convertView.findViewById(R.id.image1);
iml.displayImage(path, image);
return convertView;
}
}
为列表项添加一个布局作为R.layout.list_item_image。 这里我正在使用图像加载程序从服务器加载图像。 如果您通过本地获取图像...然后直接设置它们
希望这可以帮助你
答案 1 :(得分:0)
您可以尝试使用TwoWayView管理您的需求,以使用垂直滚动对ListView项进行水平滚动。你可以在github上同样检查this演示。
答案 2 :(得分:0)
首次活动的XML
<LinearLayout 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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".FirstActivity"
android:orientation="vertical">
<ListView
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"></ListView>
</LinearLayout>
行布局的XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<ImageView
android:id="@+id/myImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
创建此布局,然后使用自定义适配器类,您可以在图像视图中填充每行的图像...像这样的东西
自定义适配器类
public class customAdapter extends BaseAdapter {
ArrayList<HashMap<String, String>> al = new ArrayList<HashMap<String,String>>();
Activity activity;
private static LayoutInflater inflater = null;
public customAdapter(Activity a, ArrayList<HashMap<String, String>> al) {
Log.e("check", "");
activity = a;
this.al = al;
inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return al.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
imageLoader=new ImageLoader(activity.getApplicationContext());
View vi=convertView;
if(convertView==null)
{
vi = inflater.inflate(R.layout.row_layout, null);
}
ImageView myImage = (ImageView) vi.findViewById(R.id.myImage);
imageLoader.DisplayImage(//your image from resource here),myImage);
return vi;
}
}
你遇到的任何问题都可以按照我在评论中提到的教程的链接...