我正在尝试在活动中实施一个ListView
。在我的XML中,有一个ListView
用于填充我在ListView
中调用WebService
AsynkTask
的{{1}}数据。我使用BaseAdapter
来设置ListView
。现在,每当我开始这项活动时,背景会在几秒钟内变黑,然后消失。当我在ListView
长按时发生同样的事情。屏幕截图附有这个问题以及以下是我实施的代码。
主要活动的XML :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:background="@color/white"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.agraeta.user.btl.ComboOfferListActivity">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="5dp"
android:background="@color/strip_bg"
android:textColor="@color/white"
android:gravity="center"
android:textSize="17sp"
android:text="COMBO OFFERS"/>
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollingCache="false"
android:cacheColorHint="@color/white"
android:id="@+id/list_comboOffers"
android:background="@color/white"
android:divider="@android:color/transparent"
android:dividerHeight="5dp"/>
</LinearLayout>
主要活动的Java文件:
public class ComboOfferListActivity extends AppCompatActivity implements Callback<ComboOfferData> {
ListView list_comboOffers;
ComboOfferListAdapter offerListAdapter;
List<ComboOfferItem> offerItemList=new ArrayList<>();
AdminAPI adminAPI;
Custom_ProgressDialog dialog;
String userID="";
String roleID="";
AppPrefs prefs;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_combo_offer_list);
prefs=new AppPrefs(this);
adminAPI=ServiceGenerator.getAPIServiceClass();
dialog=new Custom_ProgressDialog(this,"Please Wait...");
dialog.setCancelable(false);
userID=prefs.getUserId();
roleID=prefs.getUserRoleId();
if(roleID.equals(C.ADMIN) || roleID.equals(C.COMP_SALES_PERSON) || roleID.equals(C.DISTRIBUTOR_SALES_PERSON)){
roleID=prefs.getSubSalesId();
userID=prefs.getSalesPersonId();
}
fetchIDs();
setActionBar();
}
private void fetchIDs() {
list_comboOffers=(ListView) findViewById(R.id.list_comboOffers);
offerListAdapter=new ComboOfferListAdapter(offerItemList,ComboOfferListActivity.this);
list_comboOffers.setAdapter(offerListAdapter);
list_comboOffers.setCacheColorHint(Color.TRANSPARENT);
list_comboOffers.requestFocus(0);
list_comboOffers.setScrollingCacheEnabled(false);
dialog.show();
Log.e("UR",userID+"-->"+roleID);
Call<ComboOfferData> offerDataCall=adminAPI.comboOfferDataCall(userID,roleID);
offerDataCall.enqueue(this);
}
private void setActionBar() {
// TODO Auto-generated method stub
ActionBar mActionBar = getSupportActionBar();
mActionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
mActionBar.setCustomView(R.layout.actionbar_design);
View mCustomView = mActionBar.getCustomView();
ImageView image_drawer = (ImageView) mCustomView.findViewById(R.id.image_drawer);
ImageView img_home = (ImageView) mCustomView.findViewById(R.id.img_home);
ImageView img_notification = (ImageView) mCustomView.findViewById(R.id.img_notification);
FrameLayout unread = (FrameLayout) mCustomView.findViewById(R.id.unread);
image_drawer.setImageResource(R.drawable.ic_action_btl_back);
img_home.setVisibility(View.GONE);
img_notification.setVisibility(View.GONE);
unread.setVisibility(View.GONE);
image_drawer.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onBackPressed();
}
});
mActionBar.setCustomView(mCustomView);
mActionBar.setDisplayShowCustomEnabled(true);
}
@Override
public void onResponse(Call<ComboOfferData> call, Response<ComboOfferData> response) {
dialog.dismiss();
offerItemList.clear();
ComboOfferData offerData=response.body();
if(offerData.isStatus()){
offerItemList.addAll(offerData.getOfferItemList());
}
else {
Globals.Toast2(this,offerData.getMessage());
}
Log.e("Size","-->"+offerItemList.size());
offerListAdapter.notifyDataSetChanged();
}
@Override
public void onFailure(Call<ComboOfferData> call, Throwable t) {
dialog.dismiss();
Globals.showError(t,this);
}
@Override
public void onBackPressed() {
finish();
}
}
适配器类:
public class ComboOfferListAdapter extends BaseAdapter {
List<ComboOfferItem> offerItemList=new ArrayList<>();
Activity activity;
LayoutInflater inflater;
public ComboOfferListAdapter(List<ComboOfferItem> offerItemList, Activity activity) {
this.offerItemList = offerItemList;
this.activity = activity;
inflater=activity.getLayoutInflater();
}
@Override
public int getCount() {
return offerItemList.size();
}
@Override
public Object getItem(int position) {
return offerItemList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if(convertView==null){
holder=new ViewHolder();
convertView=inflater.inflate(R.layout.layout_combooffer_list,parent,false);
holder.img_offerThumb=(ImageView) convertView.findViewById(R.id.img_offerThumb);
holder.txt_offerTitle=(TextView) convertView.findViewById(R.id.txt_offerTitle);
holder.card_comboOffer = (CardView) convertView.findViewById(R.id.card_comboOffer);
holder.layout_combo = (LinearLayout) convertView.findViewById(R.id.layout_combo);
convertView.setTag(holder);
}
else {
holder= (ViewHolder) convertView.getTag();
}
Picasso.with(activity).load(Globals.IMAGE_LINK+offerItemList.get(position).getImagePath())
.into(holder.img_offerThumb);
holder.txt_offerTitle.setText(offerItemList.get(position).getOfferTitle());
holder.img_offerThumb.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(activity, ComboOfferActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putExtra("combo_id", offerItemList.get(position).getOfferID());
activity.startActivity(intent);
}
});
holder.txt_offerTitle.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(activity, ComboOfferActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putExtra("combo_id", offerItemList.get(position).getOfferID());
activity.startActivity(intent);
}
});
return convertView;
}
@Override
public void notifyDataSetChanged() {
super.notifyDataSetChanged();
Log.e("Called","-->"+offerItemList.size());
}
private class ViewHolder {
ImageView img_offerThumb;
TextView txt_offerTitle;
CardView card_comboOffer;
LinearLayout layout_combo;
}
}
适配器视图的布局文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white"
android:orientation="vertical">
<android.support.v7.widget.CardView
android:id="@+id/card_comboOffer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:orientation="vertical"
cardBackgroundColor="#fff"
app:cardCornerRadius="2dp"
app:cardElevation="5dp"
app:theme="@style/CardView.Dark">
<LinearLayout
android:id="@+id/layout_combo"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageView
android:id="@+id/img_offerThumb"
android:layout_width="wrap_content"
android:layout_height="150dp"
android:layout_gravity="center"
android:background="?attr/selectableItemBackgroundBorderless"
android:clickable="true"
android:scaleType="fitXY"/>
<TextView
android:id="@+id/txt_offerTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#f58634"
android:padding="5dp"
android:textColor="@color/white"
android:layout_gravity="center"
android:textSize="15sp"/>
</LinearLayout>
</android.support.v7.widget.CardView>