在我的应用中,我使用的是gridview。在我的适配器中,我必须显示最后一项只有textview和背景。 我正在调整适配器中的网格项高度。
我的适配器类是
import java.util.ArrayList;
import java.util.Collections;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView.LayoutParams;
import android.widget.BaseAdapter;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.hj.app.R;
public class AddToCollectionsGridAdapter extends BaseAdapter {
ArrayList<Collections> objects;
Context mContext;
int itemHeight;
/**
*
*/
public AddToCollectionsGridAdapter(Context context,
ArrayList<Collections> objects) {
this.mContext = context;
// this.objects = objects;
}
@Override
public int getCount() {
// return objects != null ? objects.size() + 1 : 1;
return 5;
}
@Override
public Object getItem(int position) {
/*
* return (position <= objects.size() - 1) ? objects.get(position) :
* position;
*/return position;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = LayoutInflater.from(mContext).inflate(
R.layout.hj_collection_grid_item_layout, parent, false);
ViewHolder holder = new ViewHolder(convertView);
convertView.setTag(holder);
}
ViewHolder holder = (ViewHolder) convertView.getTag();
LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT,
itemHeight);
holder.parentLayout.setLayoutParams(lp);
if (position == 4) {
holder.collectionImage.setVisibility(View.GONE);
holder.collectionNameLayout.setVisibility(View.GONE);
holder.newCollectionText.setVisibility(View.VISIBLE);
holder.newCollectionText
.setBackgroundResource(R.drawable.hj_new_collection_bg);
} else {
holder.collectionImage.setVisibility(View.VISIBLE);
holder.collectionNameLayout.setVisibility(View.VISIBLE);
holder.newCollectionText.setVisibility(View.GONE);
holder.newCollectionText.setBackgroundResource(0);
}
return convertView;
}
public class ViewHolder {
public FrameLayout parentLayout;
public TextView newCollectionText;
public ImageView collectionImage;
public LinearLayout collectionNameLayout;
public ViewHolder(View view) {
newCollectionText = (TextView) view
.findViewById(R.id.add_new_collection_textView);
collectionImage = (ImageView) view.findViewById(R.id.hj_grid_image);
collectionNameLayout = (LinearLayout) view
.findViewById(R.id.collection_name_layout);
parentLayout = (FrameLayout) view.findViewById(R.id.hj_grid_parent);
}
}
/**
* @param i
*/
public void setItemHeight(int itemHeight) {
this.itemHeight = itemHeight;
}
}
每个网格项的我的布局类是
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/hj_grid_parent"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView
android:id="@+id/hj_grid_image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:contentDescription="@string/empty_string"
android:scaleType="centerCrop"
android:src="@drawable/houzz1" />
<LinearLayout
android:id="@+id/collection_name_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:background="@drawable/hj_blurr_bg"
android:gravity="bottom"
android:orientation="vertical"
android:paddingLeft="@dimen/hj_margin_medium"
android:paddingRight="@dimen/hj_margin_medium" >
<TextView
android:id="@+id/hj_grid_title_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Umesh"
android:textColor="@android:color/white"
android:textSize="@dimen/hj_font_small" />
<TextView
android:id="@+id/hj_grid_price_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Rs. 250"
android:textColor="@android:color/white"
android:textSize="@dimen/hj_font_small" />
</LinearLayout>
<TextView
android:id="@+id/add_new_collection_textView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/hj_new_collection_bg"
android:gravity="center"
android:text="@string/hj_new_collection_text"
android:textColor="@color/hj_grey_color"
android:textSize="@dimen/hj_font_medium" />
</FrameLayout>
一切正常。对于我的最后一个元素,我希望显示textview&#34; add_new_collection_textView&#34; 。根据条件可见,但我面临的问题是文本视图背景不可见。
请帮助!!!!!
EDIT1:
hj_new_collection_bg
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/hj_new_collection_pressed_bg" android:state_selected="true"></item>
<item android:drawable="@drawable/hj_new_collection_pressed_bg" android:state_pressed="true"></item>
<item android:drawable="@drawable/hj_new_collection_normal_bg"></item>
</selector>
hj_new_collection_pressed_bg
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<stroke
android:width="2dp"
android:color="#9E9E9E" />
<solid android:color="#9E9E9E" />
hj_new_collection_normal_bg
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<stroke
android:width="2dp"
android:color="#9E9E9E" />
<solid android:color="#BDBDBD" />
答案 0 :(得分:0)
您尚未指定@ drawable / hj_new_collection_bg。所以发布该文件。 它是图像还是xml文件。 同时告诉您想要的预期颜色作为背景。