我有一个显示Custom ListView的片段。片段如下 -
package com.grabstr.home;
import java.util.ArrayList;
import com.grabstr.common.Utils_Grabstr;
import com.grabstr.database.DBhelper;
import com.grabstr.search.Nearby_Search;
import com.rey.material.widget.FloatingActionButton;
import com.tech.grabstr.R;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.ListView;
import com.grabstr.home.Rewards_Data;
public class GrabItFragment extends Fragment implements FragmentCommunicator {
Activity activity;
ListView lv_grabit;
RewardsAdapter rewardsAdapter;
Context context;
LinearLayout ll_error, ll_nointernet;
private ActivityCommunicator activityCommunicator;
//String final_quary = "";
//DBhelper db_helper;
//ArrayList<Locations_data> arr_Locations;
ArrayList<Rewards_Data> arr_Rewards;
SwipeRefreshLayout swipeView;
com.rey.material.widget.ProgressView progress_wheel;
//private Drawable[] mDrawables = new Drawable[2];
public static GrabItFragment newInstance() {
GrabItFragment fragment = new GrabItFragment();
return fragment;
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
context = getActivity();
activityCommunicator = (ActivityCommunicator) context;
((Home) context).fragmentCommunicator = this;
//db_helper = new DBhelper(context);
}
@SuppressWarnings("deprecation")
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.grabit_fragment, container, false);
lv_grabit = (ListView) v.findViewById(R.id.grabItList);
swipeView = (SwipeRefreshLayout) v.findViewById(R.id.swipe);
progress_wheel = (com.rey.material.widget.ProgressView) v
.findViewById(R.id.progress_wheel);
//fab_image = (FloatingActionButton) v.findViewById(R.id.ivFilter);
ll_error = (LinearLayout) v.findViewById(R.id.ll_error);
ll_nointernet = (LinearLayout) v.findViewById(R.id.ll_nointernet);
// lv_popular.setEmptyView(ll_error);
if (!Utils_Grabstr.isNetworkAvailable(context)) {
//fab_image.setVisibility(View.GONE);
lv_grabit.setVisibility(View.GONE);
ll_error.setVisibility(View.GONE);
progress_wheel.setVisibility(View.GONE);
ll_nointernet.setVisibility(View.VISIBLE);
}
swipeView.setColorScheme(R.color.orange);
swipeView
.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
if (Utils_Grabstr.isNetworkAvailable(context)) {
swipeView.setRefreshing(true);
(new Handler()).postDelayed(new Runnable() {
@Override
public void run() {
swipeView.setRefreshing(false);
ArrayList<Rewards_Data> result = new ArrayList<Rewards_Data>();
if (rewardsAdapter == null) {
rewardsAdapter = new RewardsAdapter(
getActivity(), getActivity(),
result);
lv_grabit.setAdapter(rewardsAdapter);
ll_error.setVisibility(View.GONE);
ll_nointernet.setVisibility(View.GONE);
progress_wheel
.setVisibility(View.VISIBLE);
} else {
rewardsAdapter.update_array(result);
//fab_image.setVisibility(View.VISIBLE);
ll_error.setVisibility(View.GONE);
ll_nointernet.setVisibility(View.GONE);
progress_wheel
.setVisibility(View.VISIBLE);
rewardsAdapter.notifyDataSetChanged();
}
//final_quary = "";
//fab_image.setIcon(mDrawables[0], true);
activityCommunicator
.passDataToActivity("referesh");
}
}, 800);
}
}
});
return v;
}
@Override
public void onPause() {
super.onPause();
}
public void onResume() {
super.onResume();
if (Utils_Grabstr.isNetworkAvailable(context)) {
//if (final_quary.equalsIgnoreCase("")) {
//fab_image.setIcon(mDrawables[0], true);
ArrayList<Rewards_Data> result = ((Home) context).arr_Rewards;
if (result.size() > 0) {
ll_nointernet.setVisibility(View.GONE);
lv_grabit.setVisibility(View.VISIBLE);
if (lv_grabit.getAdapter() == null) {
rewardsAdapter = new RewardsAdapter(getActivity(),
getActivity(), result);
lv_grabit.setAdapter(rewardsAdapter);
//fab_image.setVisibility(View.VISIBLE);
ll_error.setVisibility(View.GONE);
ll_nointernet.setVisibility(View.GONE);
progress_wheel.setVisibility(View.GONE);
} else {
if (rewardsAdapter == null) {
rewardsAdapter = new RewardsAdapter(getActivity(),
getActivity(), result);
lv_grabit.setAdapter(rewardsAdapter);
progress_wheel.setVisibility(View.GONE);
//fab_image.setVisibility(View.VISIBLE);
ll_error.setVisibility(View.GONE);
} else {
rewardsAdapter.update_array(result);
rewardsAdapter.notifyDataSetChanged();
//fab_image.setVisibility(View.VISIBLE);
ll_error.setVisibility(View.GONE);
progress_wheel.setVisibility(View.GONE);
}
}
} else {
// lv_popular.setVisibility(View.GONE);
// ll_nointernet.setVisibility(View.GONE);
// progress_wheel.setVisibility(View.GONE);
// ll_error.setVisibility(View.VISIBLE);
}
} else {
//fab_image.setVisibility(View.GONE);
lv_grabit.setVisibility(View.GONE);
ll_error.setVisibility(View.GONE);
progress_wheel.setVisibility(View.GONE);
ll_nointernet.setVisibility(View.VISIBLE);
}
}
@Override
public void passDataToRewardFragment(ArrayList<Rewards_Data> result) {
// TODO Auto-generated method stub
if (result.size() > 0) {
ll_nointernet.setVisibility(View.GONE);
lv_grabit.setVisibility(View.VISIBLE);
if (lv_grabit.getAdapter() == null) {
rewardsAdapter = new RewardsAdapter(getActivity(), getActivity(),
result);
lv_grabit.setAdapter(rewardsAdapter);
//fab_image.setVisibility(View.VISIBLE);
ll_error.setVisibility(View.GONE);
progress_wheel.setVisibility(View.GONE);
} else {
if (rewardsAdapter == null) {
rewardsAdapter = new RewardsAdapter(getActivity(),
getActivity(), result);
lv_grabit.setAdapter(rewardsAdapter);
//fab_image.setVisibility(View.VISIBLE);
ll_error.setVisibility(View.GONE);
progress_wheel.setVisibility(View.GONE);
} else {
rewardsAdapter.update_array(result);
rewardsAdapter.notifyDataSetChanged();
//fab_image.setVisibility(View.VISIBLE);
ll_error.setVisibility(View.GONE);
progress_wheel.setVisibility(View.GONE);
}
}
} else {
lv_grabit.setVisibility(View.GONE);
ll_nointernet.setVisibility(View.GONE);
progress_wheel.setVisibility(View.GONE);
ll_error.setVisibility(View.VISIBLE);
}
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
// init();
}
@Override
public void passDataToFragment(ArrayList<Deal_data> result) {
// TODO Auto-generated method stub
}
}
我的适配器如下 -
package com.grabstr.home;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;
import com.grabstr.search.Special_Search;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.display.FadeInBitmapDisplayer;
import com.nostra13.universalimageloader.core.listener.ImageLoadingListener;
import com.nostra13.universalimageloader.core.listener.SimpleImageLoadingListener;
import com.tech.grabstr.R;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
public class RewardsAdapter extends BaseAdapter {
private Context context;
private Activity activity;
private ArrayList<Rewards_Data> dd;
DisplayImageOptions options;
private ImageLoadingListener animatedisplayListener = new AnimateDisplayListener();
protected ImageLoader imageLoader = ImageLoader.getInstance();
public RewardsAdapter(Context context, Activity activity,
ArrayList<Rewards_Data> messages) {
super();
this.activity = activity;
this.context = context;
this.dd = messages;
options = new DisplayImageOptions.Builder()
.showImageOnLoading(R.drawable.foodbg)
.showImageForEmptyUri(R.drawable.foodbg)
.showImageOnFail(R.drawable.foodbg).cacheInMemory(true)
.cacheOnDisc(true).considerExifParams(true).build();
}
public void update_array(ArrayList<Rewards_Data> chats_array_list) {
this.dd = chats_array_list;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return dd.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return dd.get(position);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View view = convertView;
final ViewHolder holder;
if (convertView == null) {
view = (activity).getLayoutInflater().inflate(R.layout.rewards_listrow, parent, false);
holder = new ViewHolder();
holder.rl_layParent2 = (RelativeLayout) view
.findViewById(R.id.rl_layParent2);
holder.img_reward = (ImageView) view
.findViewById(R.id.img_reward);
holder.rewardstxt_tags = (TextView) view.findViewById(R.id.rewardstxt_tags);
holder.txt_Reward = (TextView) view.findViewById(R.id.txt_Reward);
holder.txt_RewardsPercentage = (TextView) view
.findViewById(R.id.txt_RewardsPercentage);
holder.txt_RewardsLocations = (TextView) view
.findViewById(R.id.txt_RewardsLocations);
holder.txt_RewardsChance = (TextView) view
.findViewById(R.id.txt_RewardsChance);
holder.txt_Rewardstimings = (TextView) view.findViewById(R.id.txt_Rewardstimings);
view.setTag(holder);
} else {
holder = (ViewHolder) view.getTag();
}
if (!dd.get(position).getCategory().equals("")
&& dd.get(position).getCategory().length() > 2
&& !dd.get(position).getCategory().equalsIgnoreCase("undefined")) {
holder.rewardstxt_tags.setVisibility(View.VISIBLE);
holder.rewardstxt_tags.setText(dd.get(position).getCategory());
} else
holder.rewardstxt_tags.setVisibility(View.GONE);
holder.rewardstxt_tags.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent Special_Search = new Intent(context,
Special_Search.class);
Special_Search.putExtra("deal_tag", dd.get(position).getCategory());
context.startActivity(Special_Search);
}
});
if (!dd.get(position).getName().equals(""))
holder.txt_Reward.setText(dd.get(position).getName());
else
holder.txt_Reward.setText("N/A");
if(dd.get(position).getCategory().equals("weekly") || dd.get(position).getCategory().equals("monthly")
|| dd.get(position).getCategory().equals("daily")){
if(!dd.get(position).getClaims().equals(""))
holder.txt_RewardsPercentage.setText("100%");
else
{
float claims = Float.parseFloat(dd.get(position).getClaims());
float quantity = Float.parseFloat(dd.get(position).getQuantity());
float result = (quantity/claims);
String finalresult = String.format("%.2f", result);
int probability = Integer.parseInt(finalresult);
holder.txt_RewardsPercentage.setText(probability*100 + "%");
}
holder.txt_RewardsChance.setText("chance of winning");
}else
{
holder.txt_RewardsPercentage.setText(dd.get(position).getClaims());
holder.txt_RewardsChance.setText("out of "+dd.get(position).getQuantity()+" applied");
}
if (!dd.get(position).getBrand_name().equals(""))
holder.txt_RewardsLocations.setText(dd.get(position).getBrand_name());
else
holder.txt_RewardsLocations.setText("N/A");
if (!dd.get(position).getImage_url().equals("")) {
imageLoader.displayImage(dd.get(position).getImage_url(),
holder.img_reward, options, animatedisplayListener);
//Log.e("Images", "URL: " + dd.get(position).getimgURL());
}
holder.txt_Rewardstimings.setText("");
holder.rl_layParent2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
}
});
return view;
}
private class ViewHolder {
RelativeLayout rl_layParent2;
ImageView img_reward;
TextView rewardstxt_tags, txt_Reward, txt_RewardsPercentage, txt_RewardsLocations,
txt_RewardsChance, txt_Rewardstimings;
}
private static class AnimateDisplayListener extends
SimpleImageLoadingListener {
static final List<String> displayedImages = Collections
.synchronizedList(new LinkedList<String>());
@Override
public void onLoadingComplete(String imageUri, View view,
Bitmap loadedImage) {
if (loadedImage != null) {
ImageView imageView = (ImageView) view;
boolean firstDisplay = !displayedImages.contains(imageUri);
if (firstDisplay) {
FadeInBitmapDisplayer.animate(imageView, 500);
displayedImages.add(imageUri);
}
}
}
}
}
这是应用程序打开时的第一个片段,问题是在调用onResume时显示列表项,而不是在应用程序打开时显示。请你提供任何意见,我几天都坚持这个!
grabit_fragment的xml布局如下 -
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:app1="http://schemas.android.com/apk/res/com.tech.grabstr"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/fragmentbggray"
android:fillViewport="true"
android:scrollbarStyle="outsideOverlay" >
<android.support.v4.widget.SwipeRefreshLayout
android:id="@+id/swipe"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="5dp" >
<ListView
android:id="@+id/grabItList"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:divider="@android:color/transparent"
android:dividerHeight="5dp"
android:scrollbars="none" >
</ListView>
</android.support.v4.widget.SwipeRefreshLayout>
<com.rey.material.widget.ProgressView
android:id="@+id/progress_wheel"
android:layout_width="35dp"
android:layout_height="35dp"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:background="@color/orange" />
<LinearLayout
android:id="@+id/ll_error"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_weight="1"
android:gravity="center"
android:padding="5dp"
android:visibility="gone">
<ImageView android:id="@+id/imgError"
android:layout_width="60dp"
android:layout_height="60dp"
android:src="@drawable/no_deals_found"/>
<com.grabstr.fonts.MyTextView
android:id="@+id/txtNoResult"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="5dp"
android:text="OOPS. NO REWARDS FOUND!"
android:textAllCaps="true"
android:textColor="@color/text_grey_color"
android:textSize="16sp"
android:textStyle="bold" />
</LinearLayout>
<LinearLayout
android:id="@+id/ll_nointernet"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_weight="1"
android:gravity="center"
android:padding="5dp"
android:visibility="gone">
<ImageView android:id="@+id/imgnointernet"
android:layout_width="60dp"
android:layout_height="60dp"
android:src="@drawable/no_internet"/>
<com.grabstr.fonts.MyTextView
android:id="@+id/txtNoInternet"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="5dp"
android:text="INTERNET CONNECTION LOST"
android:textAllCaps="true"
android:textColor="@color/text_grey_color"
android:textSize="16sp"
android:textStyle="bold" />
</LinearLayout>
</RelativeLayout>
我在Home Activity中运行AsyncTask,调用2个片段,其中一个Fragment如上所示,onPostExecute我正在解析JSON并将其传递给片段,我正在调用passDataToReward()在上面的片段中。同时在onCreateView代码上发表评论 -
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.grabit_fragment, container, false);
lv_grabit = (ListView) v.findViewById(R.id.grabItList);
swipeView = (SwipeRefreshLayout) v.findViewById(R.id.swipe);
progress_wheel = (com.rey.material.widget.ProgressView) v
.findViewById(R.id.progress_wheel);
//fab_image = (FloatingActionButton) v.findViewById(R.id.ivFilter);
ll_error = (LinearLayout) v.findViewById(R.id.ll_error);
ll_nointernet = (LinearLayout) v.findViewById(R.id.ll_nointernet);
// lv_popular.setEmptyView(ll_error);
if (!Utils_Grabstr.isNetworkAvailable(context)) {
//fab_image.setVisibility(View.GONE);
lv_grabit.setVisibility(View.GONE);
ll_error.setVisibility(View.GONE);
progress_wheel.setVisibility(View.GONE);
ll_nointernet.setVisibility(View.VISIBLE);
}
swipeView.setColorScheme(R.color.orange);
swipeView
.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
if (Utils_Grabstr.isNetworkAvailable(context)) {
swipeView.setRefreshing(true);
(new Handler()).postDelayed(new Runnable() {
@Override
public void run() {
swipeView.setRefreshing(false);
ArrayList<Rewards_Data> result = new ArrayList<Rewards_Data>();
if (rewardsAdapter == null) {
rewardsAdapter = new RewardsAdapter(
getActivity(), getActivity(),
result);
lv_grabit.setAdapter(rewardsAdapter);
ll_error.setVisibility(View.GONE);
ll_nointernet.setVisibility(View.GONE);
progress_wheel
.setVisibility(View.VISIBLE);
} else {
rewardsAdapter.update_array(result);
//fab_image.setVisibility(View.VISIBLE);
ll_error.setVisibility(View.GONE);
ll_nointernet.setVisibility(View.GONE);
progress_wheel
.setVisibility(View.VISIBLE);
rewardsAdapter.notifyDataSetChanged();
}
//final_quary = "";
//fab_image.setIcon(mDrawables[0], true);
activityCommunicator
.passDataToActivity("referesh");
}
}, 800);
}
}
});
return v;
}