我有一个带自定义适配器的ListView。每行都有一个缩略图,Title和descroiption。 我有一个要求,列表项目,即行内容显示在屏幕的相反方向。意思是,第一行缩略图和其他内容应该在左侧,第二行内容(缩略图,标题,描述)在右侧,依此类推。
我怎样才能做到这一点?
这是我目前的代码: -
list_row.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="5dip" >
<!-- ListRow Left sied Thumbnail image -->
<LinearLayout android:id="@+id/thumbnail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="3dip"
android:layout_alignParentLeft="true"
android:layout_marginRight="5dip">
<ImageView
android:id="@+id/list_image"
android:layout_width="100dp"
android:layout_height="100dp"
android:src="@drawable/ic_launcher"/>
</LinearLayout>
<!-- Title Of Song-->
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/thumbnail"
android:layout_toRightOf="@+id/thumbnail"
android:layout_marginLeft="10dp"
android:textColor="#040404"
android:typeface="sans"
android:textSize="15dip"
android:textStyle="bold"/>
<!-- Artist Name -->
<TextView
android:id="@+id/description"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@id/title"
android:textColor="#343434"
android:layout_marginLeft="10dp"
android:textSize="10dip"
android:layout_marginTop="1dip"
android:layout_toRightOf="@+id/thumbnail"
/>
<LinearLayout
android:layout_width="130dp"
android:layout_height="wrap_content"
android:layout_below="@+id/thumbnail"
android:layout_alignParentBottom="true"
android:padding="3dip"
android:layout_alignParentLeft="true">
<TextView
android:id="@+id/txtPubDate"
android:text=""
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="10sp"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#97999C" />
</LinearLayout>
</RelativeLayout>
MyListAdapter.java
public class MyListAdapter extends BaseAdapter
{
static class LazyViewHolder
{
TextView title;
TextView description;
ImageView thumb_image;
WebView imageWebView;
TextView pubDate;
String myTitle;
String mydescription;
String bitmapUrl;
}
public MyListAdapter(Context ctx, List<News> getList)
{
mContext = ctx;
data=getList;
inflater =LayoutInflater.from(ctx);
imageLoader=new ImageLoader(ctx);
}
@Override
public int getCount()
{
// TODO Auto-generated method stub
return data.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 boolean isEnabled(int position)
{
// TODO Auto-generated method stub
return true;
}
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
View vi=convertView;
LazyViewHolder viewHolder;
if(convertView==null)
vi = inflater.inflate(R.layout.list_row,null);
viewHolder = new LazyViewHolder();
viewHolder.title = (TextView)vi.findViewById(R.id.title); // title
viewHolder.description = (TextView)vi.findViewById(R.id.description); // artist name
viewHolder.pubDate = (TextView)vi.findViewById(R.id.txtPubDate);
News song = data.get(position);
myTitle =song.getTitle();
//Set description
mydescription =Html.fromHtml(song.getBody()).toString();
// Set image
bitmapUrl = song.getBigImage();
//Set date
myDate = song.getPublishedDate();
StringBuilder sb = new StringBuilder(myTitle);
int i = 0;
while ((i = sb.indexOf(" ", i + 40)) != -1) {
sb.replace(i, i + 1, "\n");
}
viewHolder.title.setText(sb.toString());
mydescription = mydescription.substring(0, Math.min(mydescription.length(), 50));
viewHolder.description.setText(mydescription);
viewHolder.thumb_image=(ImageView)vi.findViewById(R.id.list_image); // thumb image
imageLoader.DisplayImage(bitmapUrl, viewHolder.thumb_image);
return vi;
}
}
答案 0 :(得分:0)
你的行xml和getview中可以有两个相对布局,你可以这样做
if(position % 2 == 0)
{
// This will be true for 0, 2, 4... rows
// make firstRelativeView Gone
// make secondRelativeView Visible
}
else
{
//This will be true for 1,3,5,,.. rows
// make firstRelativeView Visible
// make secondRelativeView Gone
}
relativeLayouts都有缩略图,标题和描述,但方向相反。在此if else本身设置您的图像,标题和描述
此外,您可以点击以下链接。这是一个关于如何在列表视图中充气多个视图
的教程答案 1 :(得分:0)
第一种方式:
创建两个行布局,一个是左侧的图像,另一个是右侧的图像。在您的适配器中,覆盖getViewTypeCount()
并返回2
。覆盖getItemViewType(int position)
并为一种行布局返回0
,为另一种返回1
(如果需要,可以定义常量,但这些是需要返回的值)。最后,在getView()
中,您可以针对给定的位置调用getItemViewType()
,并针对返回的值调用switch
。这决定了要膨胀的行布局等。
第二种方式:
使用android:visibility="gone"
两侧的图像进行一行布局。对所有行使用相同的布局,并使用setVisibility(View.VISIBLE)
使两个图像中的一个可见。根据需要选择要显示的图像。 (确保在另一张图片上调用setVisibility(View.GONE)
,以防此行被回收并且之前的其他图片可见。)
答案 2 :(得分:0)
您需要定义2个布局,一个用于左侧,一个用于右侧,相同的组件ID 。
比你的适配器(假设你正在使用光标适配器)
private int getItemViewType(Cursor cursor) {
//some method that returns 1 for left layout, 0 for right layout
}
@Override
public int getViewTypeCount() {
return 2; //return 2 view types
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
ViewHolder viewHolder = (ViewHolder) view.getTag();
//bind your view
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
int type = getItemViewType(cursor);
int layout;
switch (type) {
case 1:
layout = R.layout.row_left; //left layout
break;
case 0:
layout = R.layout.row_right; //right layout
break;
default:
throw new RuntimeException("Unknown view type!");
}
View view = LayoutInflater.from(context).inflate(layout, parent, false);
ViewHolder viewHolder = new ViewHolder();
//initialize ViewHolder using same ids.
view.setTag(viewHolder);
return view;
}
答案 3 :(得分:0)
// Try this way,hope this will help you to solve your problem...
**list_row.xml**
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:id="@+id/lnrLeft"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginRight="30dp">
<ImageView
android:id="@+id/imgLeft"
android:layout_width="100dp"
android:layout_height="100dp"
android:src="@drawable/ic_launcher"/>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical"
android:layout_marginLeft="10dp">
<TextView
android:id="@+id/txtLeftTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#040404"
android:typeface="sans"
android:textSize="15sp"
android:textStyle="bold"/>
<TextView
android:id="@+id/txtLeftDescription"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#343434"
android:textSize="12sp"
android:layout_marginTop="5dp"/>
<TextView
android:id="@+id/txtLeftPubDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="12sp"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#97999C"
android:layout_marginTop="5dp"/>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:id="@+id/lnrRight"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="30dp">
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical"
android:layout_marginLeft="10dp">
<TextView
android:id="@+id/txtRightTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#040404"
android:typeface="sans"
android:textSize="15sp"
android:textStyle="bold"/>
<TextView
android:id="@+id/txtRightDescription"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#343434"
android:textSize="12sp"
android:layout_marginTop="5dp"/>
<TextView
android:id="@+id/txtRightPubDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="12sp"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#97999C"
android:layout_marginTop="5dp"/>
</LinearLayout>
<ImageView
android:id="@+id/imgRight"
android:layout_width="100dp"
android:layout_height="100dp"
android:src="@drawable/ic_launcher"/>
</LinearLayout>
</LinearLayout>
**MyListAdapter.java**
public class MyListAdapter extends BaseAdapter
{
static class LazyViewHolder
{
LinearLayout lnrLeft;
ImageView imgLeft;
TextView txtLeftTitle;
TextView txtLeftDescription;
TextView txtLeftPubDate;
LinearLayout lnrRight;
ImageView imgRight;
TextView txtRightTitle;
TextView txtRightDescription;
TextView txtRightPubDate;
}
private Context context;
private List<News> newsList;
private ImageLoader imageLoader;
public MyListAdapter(Context context, List<News> newsList)
{
context = context;
newsList=newsList;
imageLoader=new ImageLoader(context);
}
@Override
public int getCount()
{
return newsList.size();
}
@Override
public Object getItem(int position)
{
return newsList.get(position);
}
@Override
public long getItemId(int position)
{
return position;
}
@Override
public boolean isEnabled(int position)
{
return true;
}
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
LazyViewHolder viewHolder;
if(convertView==null) {
convertView = LayoutInflater.from(context).inflate(R.layout.list_row, null,false);
viewHolder = new LazyViewHolder();
viewHolder.lnrLeft = (LinearLayout) convertView.findViewById(R.id.lnrLeft);
viewHolder.imgLeft = (ImageView) convertView.findViewById(R.id.imgLeft);
viewHolder.txtLeftTitle = (TextView) convertView.findViewById(R.id.txtLeftTitle);
viewHolder.txtLeftDescription = (TextView) convertView.findViewById(R.id.txtLeftDescription);
viewHolder.txtLeftPubDate = (TextView) convertView.findViewById(R.id.txtLeftPubDate);
viewHolder.lnrRight= (LinearLayout) convertView.findViewById(R.id.lnrRight);
viewHolder.imgRight = (ImageView) convertView.findViewById(R.id.imgRight);
viewHolder.txtRightTitle = (TextView) convertView.findViewById(R.id.txtRightTitle);
viewHolder.txtRightDescription = (TextView) convertView.findViewById(R.id.txtRightDescription);
viewHolder.txtRightPubDate = (TextView) convertView.findViewById(R.id.txtRightPubDate);
convertView.setTag(viewHolder);
}else{
viewHolder = (LazyViewHolder) convertView.getTag();
}
StringBuilder sb = new StringBuilder(data.get(position).getTitle());
int i = 0;
while ((i = sb.indexOf(" ", i + 40)) != -1) {
sb.replace(i, i + 1, "\n");
}
if(position%2==0){
viewHolder.lnrLeft.setVisibility(View.VISIBLE);
viewHolder.lnrRight.setVisibility(View.GONE);
imageLoader.DisplayImage(data.get(position).getBigImage(), viewHolder.imgLeft);
viewHolder.txtLeftTitle.setText(sb.toString());
viewHolder.txtLeftDescription.setText(Html.fromHtml(data.get(position).getBody()).toString());
viewHolder.txtLeftPubDate.setText(data.get(position).getPublishedDate());
}else{
viewHolder.lnrRight.setVisibility(View.VISIBLE);
viewHolder.lnrLeft.setVisibility(View.GONE);
imageLoader.DisplayImage(data.get(position).getBigImage(), viewHolder.imgRight);
viewHolder.txtRightTitle.setText(sb.toString());
viewHolder.txtRightDescription.setText(Html.fromHtml(data.get(position).getBody()).toString());
viewHolder.txtRightPubDate.setText(data.get(position).getPublishedDate());
}
return convertView;
}
}