如何实现UIL加载图像和文本列表视图点击查看订阅者?

时间:2015-04-27 18:49:00

标签: android json listview android-viewpager

我设法创建了一个使用Universal Image Loader加载图片和文字的列表视图。但是如何实现点击时显示的UIL JSON列表视图会变成viewpager?请帮帮我.... 这是我的代码:

请帮助我......

这是我的代码:

public class NewsFragment extends AbsListViewBaseFragment{
public static final String TAG = NewsFragment.class.getSimpleName();        
public static final String NEWS_TITLE = "News"; // buat judul saat ini
public static final String imageUrl = "http://192.168.1.6/laravel-master/public/img/post/";
private static final String NEWS_SCHEME = "settings";
private static final String NEWS_AUTHORITY = "news";
// url to make request
private static String url = "http://192.168.1.6/laravel-master/public/mobile/pesan/0079";
// key JSON
private static String KEY_SUCCESS = "success";
// JSON Node names from url
private static final String TAG_DATA = "data";
private static final String TAG_PID = "id";
private static final String TAG_TITLE = "title";
private static final String TAG_CONTENT = "text";// persiapan content
private static final String TAG_IMAGES = "url"; 
// JSON Array Data News
JSONArray news = null;
// terima JSON key      
public static final Uri NEWS_URI = new Uri.Builder()
  .scheme(NEWS_SCHEME)
  .authority(NEWS_AUTHORITY)
  .build();
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup viewGroup, Bundle savedBundle){
    final View v = inflater.inflate(R.layout.news, viewGroup, false);
    // inisial newslist
    ArrayList<HashMap<String, String>> newsList = new ArrayList<HashMap<String,String>>();
    // create json parser instance
    JSONParserComplete jsonParser = new JSONParserComplete();
    // GET JSONObject from url
    JSONObject json = jsonParser.makeHttpRequest(url, "GET");
    try {
        if(json.getString(KEY_SUCCESS)!= null){
            news = json.getJSONArray(TAG_DATA);
            Log.d("news:",news.toString());
            for (int i = 0; i < news.length(); i++) {
                JSONObject c    = news.getJSONObject(i);
                String id       = c.getString(TAG_PID);
                String title    = c.getString(TAG_TITLE);
                String content  = c.getString(TAG_CONTENT);
                String images   = c.getString(TAG_IMAGES);
                // create new hasmap :)
                HashMap<String, String> map = new HashMap<String, String>();
                map.put(TAG_PID, id);
                map.put(TAG_TITLE, title);
                map.put(TAG_CONTENT, content);
                map.put(TAG_IMAGES, images);
                // add map to arraylist
                newsList.add(map);              
            }

        }
    } catch (JSONException e) {
        e.printStackTrace();
    }
    // make listview
    listView = (ListView)v.findViewById(android.R.id.list);
    ((ListView)listView).setAdapter(new ListImageAdapter(getActivity(), newsList));
    return v;   
}
private static class ListImageAdapter extends BaseAdapter{
    private LayoutInflater inflater;
    private static final String TAG_PID = "id";
    private static final String TAG_TITLE = "title";
    private static final String TAG_CONTENT = "text";
    private static final String TAG_IMAGES = "url";
    private ArrayList<HashMap<String,String>> data;
    private ImageLoadingListener animateFirstListener = new AnimateFirstDisplayListener();

    private DisplayImageOptions options;
    private Context context;

    ListImageAdapter(Context context, ArrayList<HashMap<String,String>> data){
        this.context = context;
        //inflater = LayoutInflater.from(context);
        this.data = data;
        options = new DisplayImageOptions.Builder()
                .showImageOnLoading(R.drawable.ic_stub)
                .showImageForEmptyUri(R.drawable.ic_empty)
                .showImageOnFail(R.drawable.ic_error)
                .cacheInMemory(true)
                .cacheOnDisk(true)
                .considerExifParams(true)
                .displayer(new RoundedBitmapDisplayer(20)).build();
        Log.d("news:","ListImageContext");

    }

    @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 arg0) {
        // TODO Auto-generated method stub
        return arg0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolderList holder;
        View view = convertView;
        if (convertView == null) {
            holder = new ViewHolderList();
            view = LayoutInflater.from(context).inflate(R.layout.list_newsitem, parent,false);
            holder.icon = (ImageView) view.findViewById(R.id.iconList);
            holder.txtTid = (TextView) view.findViewById(R.id.pid);
            holder.txtTitle = (TextView) view.findViewById(R.id.title);
            // post disembunyikan
            holder.txtContent = (TextView) view.findViewById(R.id.content);

            view.setTag(holder);
        }else{
            holder = (ViewHolderList) view.getTag();
        }
        final String pid = data.get(position).get(TAG_PID);
        final String title = data.get(position).get(TAG_TITLE);
        final String content = data.get(position).get(TAG_CONTENT);
        final String img = imageUrl + data.get(position).get(TAG_IMAGES);

        holder.txtTid.setText(pid);
        holder.txtTitle.setText(title);
        holder.txtContent.setText(content);
        Log.d("IMAGELOADER:","GETVIEW");     
       ImageLoader.getInstance().displayImage(img, holder.icon, options, animateFirstListener);

       holder.icon.setFocusable(false);
        holder.txtTid.setFocusable(false);
        holder.txtTitle.setFocusable(false);
        holder.txtContent.setFocusable(false);
            view.setFocusable(true);
            view.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View arg0) {
                    // TODO Auto-generated method stub
                    Intent intent = new Intent(context, NewsItemPagerActivity.class);
                    intent.putExtra("BERITA", data.toArray());
                    //intent.putExtra(TAG_PID, pid);
                    //intent.putExtra(TAG_TITLE, title);
                    //intent.putExtra(TAG_CONTENT, content);
                    //intent.putExtra(TAG_IMAGES, img); 
                    context.startActivity(intent);                                      
                }               
            });
          return view;
    }
    public class ViewHolderList {
         ImageView icon;
         TextView txtTid;
         TextView txtTitle;
         TextView txtContent;    
    }
}
private static class AnimateFirstDisplayListener 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);
            }
        }
    }
}

}

我应该如何实现这些viewpager?

0 个答案:

没有答案