我正在开发一个自定义listview
,根据number of row
数据填充second row
中的HashMap
Adapter.java
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = context.getLayoutInflater();
itemsData = hasMap.get(position);
ViewHolder viewHolder;
View view = null;
if (convertView == null) {
viewHolder = new ViewHolder();
convertView = inflater.inflate(R.layout.listview_row_storewise_details, null, false);
viewHolder.txt_building_name = (TextView) convertView.findViewById(R.id.BUILDING);
viewHolder.txt_tower_name = (TextView) convertView.findViewById(R.id.TOWER);
viewHolder.txt_flat_number = (TextView) convertView.findViewById(R.id.FLAT_NO);
viewHolder.txt_order_id = (TextView) convertView.findViewById(R.id.ORDER_NO);
viewHolder.txt_delivery_time = (TextView) convertView.findViewById(R.id.Datetime);
viewHolder.txt_grand_total = (TextView) convertView.findViewById(R.id.txt_recurring_total_price5);
viewHolder.container = (LinearLayout) convertView.findViewById(R.id.container);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.container.removeAllViews();
for (int i=0;i<itemsData.size();i++){
StorewiseDetailsModel test = itemsData.get(i);
Log.d(TAG," test "+itemsData.get(i).getStore_name());
view = inflater.inflate(R.layout.storewise_detail_orderrow, null);
viewHolder.txt_not_vailable = (TextView) view.findViewById(R.id.txt_not_vailable);
viewHolder.txt_rupee_symbol1 = (TextView) view.findViewById(R.id.txt_recurring_price_sym_qnty);
viewHolder.txt_rupee_symbol2 = (TextView) view.findViewById(R.id.txt_recurring_price_sym1);
viewHolder.txt_item_name = (TextView) view.findViewById(R.id.txt_product_name);
viewHolder.txt_item_size = (TextView) view.findViewById(R.id.txt_product_weight);
viewHolder.txt_item_quantity = (TextView) view.findViewById(R.id.txt_recurring_total_price_qnty);
viewHolder.txt_item_total = (TextView) view.findViewById(R.id.txt_recurring_total_price);
viewHolder.divider = (TextView) view.findViewById(R.id.divider);
viewHolder.container.addView(view);
viewHolder.txt_grand_total.setText(test.getTotal());
viewHolder.txt_building_name.setText(test.getBuilding_name());
viewHolder.txt_tower_name.setText(test.getTower_name());
viewHolder.txt_flat_number.setText(test.getFlat_number());
viewHolder.txt_order_id.setText(test.getOrder_id());
viewHolder.txt_delivery_time.setText(utilClass.formatDate(test.getDelivery_time()));
if (test.getIs_available().equals("0")){
viewHolder.txt_not_vailable.setVisibility(View.VISIBLE);
viewHolder.txt_item_name.setTextColor(context.getResources().getColor(R.color.red_text));
viewHolder.txt_item_size.setTextColor(context.getResources().getColor(R.color.red_text));
viewHolder.txt_item_quantity.setTextColor(context.getResources().getColor(R.color.red_text));
viewHolder.txt_item_total.setTextColor(context.getResources().getColor(R.color.red_text));
viewHolder.txt_rupee_symbol1.setTextColor(context.getResources().getColor(R.color.red_text));
viewHolder.txt_rupee_symbol2.setTextColor(context.getResources().getColor(R.color.red_text));
viewHolder.txt_item_name.setText(test.getItem_name());
viewHolder.txt_item_size.setText(test.getItem_size());
viewHolder.txt_item_quantity.setText(test.getItem_price() + " x" + test.getItem_quantity());
viewHolder.txt_item_total.setText(test.getItem_total());
}
else if (test.getIs_available().equals("1")){
viewHolder.txt_not_vailable.setVisibility(View.GONE);
viewHolder.txt_item_name.setTextColor(context.getResources().getColor(R.color.black_text));
viewHolder.txt_item_size.setTextColor(context.getResources().getColor(R.color.black_text));
viewHolder.txt_item_quantity.setTextColor(context.getResources().getColor(R.color.black_text));
viewHolder.txt_item_total.setTextColor(context.getResources().getColor(R.color.black_text));
viewHolder.txt_rupee_symbol1.setTextColor(context.getResources().getColor(R.color.black_text));
viewHolder.txt_rupee_symbol2.setTextColor(context.getResources().getColor(R.color.black_text));
viewHolder.txt_item_name.setText(test.getItem_name());
viewHolder.txt_item_size.setText(test.getItem_size());
viewHolder.txt_item_quantity.setText(test.getItem_price() + " x" + test.getItem_quantity());
viewHolder.txt_item_total.setText(test.getItem_total());
}
}
return convertView;
}
class ViewHolder {
TextView txt_building_name;
TextView txt_tower_name;
TextView txt_flat_number;
TextView txt_order_id;
TextView txt_delivery_time;
TextView txt_grand_total;
TextView txt_item_name;
TextView txt_item_size;
TextView txt_item_quantity;
TextView txt_item_total;
LinearLayout container;
TextView divider;
TextView txt_not_vailable;
TextView txt_rupee_symbol1,txt_rupee_symbol2;
}
listview_row_storewise_details.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#eeeeee"
android:paddingTop="10dp"
android:paddingBottom="10dp"
android:paddingLeft="15dp"
android:paddingRight="10dp"
>
<RelativeLayout
android:id="@+id/relativeLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true">
<com.techmorphosis.Utils.TextViewCustomFont
android:id="@+id/txt_not_vailable"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="1dp"
android:text="not available"
android:visibility="gone"
android:textColor="#b8b8b8"
android:drawableLeft="@drawable/ic_not_available"
android:drawablePadding="5dp"
android:textSize="@dimen/textsize_large" />
<com.techmorphosis.Utils.TextViewCustomFont
android:id="@+id/txt_product_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="1dp"
android:layout_below="@+id/txt_not_vailable"
android:textSize="@dimen/textsize_large"
android:textColor="#000000" />
<com.techmorphosis.Utils.TextViewCustomFont
android:id="@+id/txt_product_weight"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/txt_product_name"
android:padding="0dp"
android:textSize="@dimen/textsize_large"
android:textColor="#000000"
/>
</RelativeLayout>
<RelativeLayout
android:id="@+id/relative_order_confirm_price"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignBottom="@+id/relativeLayout"
android:paddingLeft="10dp">
<LinearLayout
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="right"
>
<RelativeLayout
android:id="@+id/layoutVoice1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="0.6"
android:layout_gravity="bottom"
>
<com.techmorphosis.Utils.TextViewCustomFont
android:id="@+id/txt_recurring_total_price_qnty"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/txt_recurring_price_sym_qnty"
android:padding="2dp"
android:layout_centerVertical="true"
android:textSize="@dimen/textsize_large"
android:textColor="@color/black_text" />
<com.techmorphosis.Utils.TextViewCustomFontBold
android:id="@+id/txt_recurring_price_sym_qnty"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="0dp"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:text="@string/rupees_sym"
android:textSize="@dimen/textsize_large"
android:textColor="@color/black_text"/>
</RelativeLayout>
<View
android:id="@+id/view1"
android:background="@color/uiv3_divider_color"
android:layout_width="1.0dip"
android:layout_height="fill_parent"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
/>
<RelativeLayout
android:gravity="left"
android:id="@+id/layoutScanner1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="0.6"
android:paddingLeft="10dp"
>
<com.techmorphosis.Utils.TextViewCustomFont
android:id="@+id/txt_recurring_total_price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:padding="2dp"
android:layout_centerVertical="true"
android:textSize="@dimen/textsize_large"
android:textColor="@color/black_text" />
<com.techmorphosis.Utils.TextViewCustomFontBold
android:id="@+id/txt_recurring_price_sym1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="@+id/txt_recurring_total_price"
android:padding="2dp"
android:layout_centerVertical="true"
android:text="@string/rupees_sym"
android:textSize="@dimen/textsize_large"
android:textColor="@color/black_text"/>
</RelativeLayout>
</LinearLayout>
</RelativeLayout>
</RelativeLayout>
<View
android:layout_width="match_parent"
android:layout_height="0.8dp"
android:background="#bebebe" />
</LinearLayout>
storewise_detail_orderrow.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">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="0dp"
android:orientation="vertical">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#eeeeee"
android:paddingBottom="10dp"
android:paddingLeft="15dp"
android:paddingRight="15dp"
android:paddingTop="10dp"
>
<com.techmorphosis.Utils.TextViewCustomFont
android:id="@+id/BUILDING"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:textColor="#5330A8"
android:textSize="@dimen/textsize_large"
/>
<com.techmorphosis.Utils.TextViewCustomFont
android:id="@+id/TOWER"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/BUILDING"
android:textColor="#5330A8"
android:textSize="@dimen/textsize_large" />
<com.techmorphosis.Utils.TextViewCustomFont
android:id="@+id/dash"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/BUILDING"
android:layout_toRightOf="@+id/TOWER"
android:text="-"
android:textColor="#5330A8"
android:textSize="@dimen/textsize_large" />
<com.techmorphosis.Utils.TextViewCustomFont
android:id="@+id/FLAT_NO"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/BUILDING"
android:layout_toRightOf="@+id/dash"
android:textColor="#5330A8"
android:textSize="@dimen/textsize_large" />
<com.techmorphosis.Utils.TextViewCustomFont
android:id="@+id/ORDER_NO"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:gravity="right"
android:textColor="#5330A8"
android:textSize="@dimen/textsize_large"
/>
<com.techmorphosis.Utils.TextViewCustomFont
android:id="@+id/Datetime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="@+id/ORDER_NO"
android:textColor="#5330A8"
android:textSize="@dimen/textsize_large" />
</RelativeLayout>
<View
android:layout_width="match_parent"
android:layout_height="0.8dp"
android:background="#bebebe" />
<LinearLayout
android:id="@+id/container"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
</LinearLayout>
<LinearLayout
android:id="@+id/myrow4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#eeeeee"
android:orientation="horizontal"
android:paddingBottom="15dp"
android:paddingTop="15dp"
android:weightSum="1"
>
<com.techmorphosis.Utils.TextViewCustomFont
android:layout_width="0.0dip"
android:layout_height="wrap_content"
android:layout_weight="0.50"
android:gravity="center"
android:textColor="#000000"
android:textSize="20sp"
/>
<RelativeLayout
android:id="@+id/relative_order_confirm_price4"
android:layout_width="0.0dip"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_weight="0.50"
android:paddingLeft="10dp"
android:paddingRight="17dp">
<com.techmorphosis.Utils.TextViewCustomFont
android:id="@+id/txt_recurring_total_price5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:padding="2dp"
android:text="0"
android:textColor="@color/black_text"
android:textSize="20sp"
/>
<com.techmorphosis.Utils.TextViewCustomFontBold
android:id="@+id/txt_recurring_price_sym5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toLeftOf="@+id/txt_recurring_total_price5"
android:padding="0dp"
android:text="@string/rupees_sym"
android:textColor="@color/black_text"
android:textSize="@dimen/textsize_large" />
<com.techmorphosis.Utils.TextViewCustomFont
android:id="@+id/txt_recurring_total_price_qnty5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginLeft="35dp"
android:padding="2dp"
android:text="TOTAL"
android:textColor="@color/black_text"
android:textSize="20sp" />
</RelativeLayout>
</LinearLayout>
</LinearLayout>
<View android:id="@+id/view"
android:layout_width="match_parent"
android:layout_height="15dp"
android:background="@color/white_background"
/>
</LinearLayout>
答案 0 :(得分:3)
嵌套列表不是好设计理念。
它应该首先列出父项,并且在选择父项时,它应该列出具有页眉和页脚的子项。
如果您想继续使用此设计,可以尝试转换为RecyclerView,这比ListView
更有效。
如果您仍想继续使用ListView
优化列表适配器的getView()
方法,如下所示,以提高性能。
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = context.getLayoutInflater();
//Make this variable local in methods not need to be global
itemsData = hasMap.get(position);
ViewHolder viewHolder;
View view = null;
if (convertView == null) {
viewHolder = new ViewHolder();
convertView = inflater.inflate(R.layout.listview_row_storewise_details, null, false);
viewHolder.txt_building_name = (TextView) convertView.findViewById(R.id.BUILDING);
viewHolder.txt_tower_name = (TextView) convertView.findViewById(R.id.TOWER);
viewHolder.txt_flat_number = (TextView) convertView.findViewById(R.id.FLAT_NO);
viewHolder.txt_order_id = (TextView) convertView.findViewById(R.id.ORDER_NO);
viewHolder.txt_delivery_time = (TextView) convertView.findViewById(R.id.Datetime);
viewHolder.txt_grand_total = (TextView) convertView.findViewById(R.id.txt_recurring_total_price5);
viewHolder.container = (LinearLayout) convertView.findViewById(R.id.container);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
int redTextColor = context.getResources().getColor(R.color.red_text);
int blackTextColor = context.getResources().getColor(R.color.black_text);
viewHolder.container.removeAllViews();
for (int i=0;i<itemsData.size();i++){
StorewiseDetailsModel test = itemsData.get(i);
Log.d(TAG," test "+itemsData.get(i).getStore_name());
view = inflater.inflate(R.layout.storewise_detail_orderrow, null);
viewHolder.container.addView(view);
TextView txt_not_vailable = (TextView) view.findViewById(R.id.txt_not_vailable);
TextView txt_rupee_symbol1 = (TextView) view.findViewById(R.id.txt_recurring_price_sym_qnty);
TextView txt_rupee_symbol2 = (TextView) view.findViewById(R.id.txt_recurring_price_sym1);
TextView txt_item_name = (TextView) view.findViewById(R.id.txt_product_name);
TextView txt_item_size = (TextView) view.findViewById(R.id.txt_product_weight);
TextView txt_item_quantity = (TextView) view.findViewById(R.id.txt_recurring_total_price_qnty);
TextView txt_item_total = (TextView) view.findViewById(R.id.txt_recurring_total_price);
TextView divider = (TextView) view.findViewById(R.id.divider);
//These attribute should be only once per row. Should not repeat in all "item data"
viewHolder.txt_grand_total.setText(test.getTotal());
viewHolder.txt_building_name.setText(test.getBuilding_name());
viewHolder.txt_tower_name.setText(test.getTower_name());
viewHolder.txt_flat_number.setText(test.getFlat_number());
viewHolder.txt_order_id.setText(test.getOrder_id());
viewHolder.txt_delivery_time.setText(utilClass.formatDate(test.getDelivery_time()));
if (test.getIs_available().equals("0")){
txt_not_vailable.setVisibility(View.VISIBLE);
txt_item_name.setTextColor(redTextColor);
txt_item_size.setTextColor(redTextColor);
txt_item_quantity.setTextColor(redTextColor);
txt_item_total.setTextColor(redTextColor);
txt_rupee_symbol1.setTextColor(redTextColor);
txt_rupee_symbol2.setTextColor(redTextColor);
txt_item_name.setText(test.getItem_name());
txt_item_size.setText(test.getItem_size());
txt_item_quantity.setText(test.getItem_price() + " x" + test.getItem_quantity());
txt_item_total.setText(test.getItem_total());
}
else if (test.getIs_available().equals("1")){
txt_not_vailable.setVisibility(View.GONE);
txt_item_name.setTextColor(blackTextColor);
txt_item_size.setTextColor(blackTextColor);
txt_item_quantity.setTextColor(blackTextColor);
txt_item_total.setTextColor(blackTextColor);
txt_rupee_symbol1.setTextColor(blackTextColor);
txt_rupee_symbol2.setTextColor(blackTextColor);
txt_item_name.setText(test.getItem_name());
txt_item_size.setText(test.getItem_size());
txt_item_quantity.setText(test.getItem_price() + " x" + test.getItem_quantity());
txt_item_total.setText(test.getItem_total());
}
}
return convertView;
}
class ViewHolder {
TextView txt_building_name;
TextView txt_tower_name;
TextView txt_flat_number;
TextView txt_order_id;
TextView txt_delivery_time;
TextView txt_grand_total;
LinearLayout container;
}
另外,正如我在代码注释中指定的,更改数据结构将有所帮助,从“项目数据”中删除诸如构建名称,总计...之类的公共属性,并将它们包含在具有“项目数据”的ArrayList的另一个类中
答案 1 :(得分:2)
我很抱歉,因为声誉低,我无法发表评论。我没有明确的答案,但只是简单地查看您的代码,我可以看到您已经使用findViewById()
了很多。因此,它可能会影响您的表现。
您可能应该修改代码,以便Adapter
不必为每个项目找到每个项目的视图。
编辑:
好的,所以我修改了你的ViewHolder,以便在构造函数本身内部查找视图,从而为你节省了每次插入视图所需的麻烦和计算费用。
class ViewHolder {
TextView txt_building_name;
TextView txt_tower_name;
TextView txt_flat_number;
TextView txt_order_id;
TextView txt_delivery_time;
TextView txt_grand_total;
TextView txt_item_name;
TextView txt_item_size;
TextView txt_item_quantity;
TextView txt_item_total;
LinearLayout container;
TextView divider;
TextView txt_not_vailable;
TextView txt_rupee_symbol1, txt_rupee_symbol2;
public ViewHolder(View convertView, View view) {
txt_building_name = (TextView) convertView.findViewById(R.id.BUILDING);
txt_tower_name = (TextView) convertView.findViewById(R.id.TOWER);
txt_flat_number = (TextView) convertView.findViewById(R.id.FLAT_NO);
txt_order_id = (TextView) convertView.findViewById(R.id.ORDER_NO);
txt_delivery_time = (TextView) convertView.findViewById(R.id.Datetime);
txt_grand_total = (TextView) convertView.findViewById(R.id.txt_recurring_total_price5);
txt_item_name = (TextView) view.findViewById(R.id.txt_product_name);
txt_item_size = (TextView) view.findViewById(R.id.txt_product_weight);
txt_item_quantity = (TextView) view.findViewById(R.id.txt_recurring_total_price_qnty);
txt_item_total = (TextView) view.findViewById(R.id.txt_recurring_total_price);
divider = (TextView) view.findViewById(R.id.divider);
txt_not_vailable = (TextView) view.findViewById(R.id.txt_not_vailable);
txt_rupee_symbol1 = (TextView) view.findViewById(R.id.txt_recurring_price_sym_qnty);
txt_rupee_symbol2 = (TextView) view.findViewById(R.id.txt_recurring_price_sym1);
}
}
此处,convertView
是您从listview_row_storewise_details
充气的容器,而view
是您从storewise_detail_orderrow
充气的另一个容器。
请尝试使用此ViewHolder
并告诉我它是否可以减少滞后。
答案 2 :(得分:1)
尝试使用recyclerview
充气2种不同的布局