如何将两个页脚添加到一个列表视图中

时间:2016-12-21 08:41:59

标签: android listview

我想创建一个应用,因为你可以看到照片app web version

要做这个新闻淋浴应用程序,视频标题后有正常新闻和视频新闻项目的列表视图项目 我创建了一个listview来执行此操作,并为此listview添加了两个适配器。正常新闻的适配器之一和视频新闻的第二适配器。但我需要添加图片中显示的按钮" Daha fazla haber"此按钮可为正常新闻添加更多新闻,我还需要视频新闻列表视图项下的第二个新闻按钮。因此,如果用户点击更多新闻按钮,那么应用将在视频新闻项之前的正常新闻项目之后显示更多新闻这该怎么做?可以在同一个列表视图中制作吗?

我的xml就是那样

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
<ListView
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:id="@+id/haberList"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:divider="@android:color/white"
    android:background="@android:color/white"
    android:dividerHeight="2.0sp" />
</RelativeLayout>

我的适配器就是那样

MergeAdapter mergeAdapter;
private class sunucudanGetir extends AsyncTask<Void, Void, Void> {
    ProgressDialog pDialog;
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        pDialog = new ProgressDialog(getActivity());
        pDialog.setMessage("Haberler alınıyor...");
        pDialog.setIndeterminate(false);
        pDialog.setCancelable(true);
        pDialog.show();
    }

    @Override
    protected Void doInBackground(Void... params) {

        try {
            List<NameValuePair> params2 = new ArrayList<NameValuePair>();
            JSONParser jsonParser = new JSONParser();
            JSONObject json = jsonParser.makeHttpRequest(MainActivity.web_site + "mobil-api/ana-haberler", "GET", params2);

            JSONArray jar = json.getJSONArray("haberler");

            for (int i = 0; ( i<5); i++) {//i < jar.length()||

                JSONObject j = jar.getJSONObject(i);

                Haber temp = new Haber();
                temp.setId(j.getInt("haberId"));
                temp.setBaslik(j.getString("baslik"));
                temp.setVideo(j.getInt("video"));
                //if(j.getInt("video")==1)
                //    temp.setResimurl("https://i.ytimg.com/vi/"+ j.getString("embed") +"/hqdefault.jpg?custom=true&w=196&h=110");
                //else
                temp.setResimurl(j.getString("resimUrl"));
                haberList.add(temp);
                //Log.d("Haber:",temp.toString());

            }


            Haber x= new Haber();
            x.setId(0);
            basliklist.add(x);



            List<NameValuePair> paramsvideo = new ArrayList<NameValuePair>();
            JSONParser jsonParservideo = new JSONParser();

            JSONObject jsonvideo = jsonParservideo.makeHttpRequest("http://www.uretenankara.com/mobil-api/video-ana-haberler", "GET", paramsvideo);

            JSONArray jarvideo = jsonvideo.getJSONArray("haberler");

            for (int i = 0; ( i<5); i++) {

                JSONObject j_video = jarvideo.getJSONObject(i);

                Haber tempvideo = new Haber();
                tempvideo.setId(j_video.getInt("haberId"));
                tempvideo.setBaslik(j_video.getString("baslik"));
                tempvideo.setVideo(j_video.getInt("video"));
                //if(j.getInt("video")==1)
                //    temp.setResimurl("https://i.ytimg.com/vi/"+ j.getString("embed") +"/hqdefault.jpg?custom=true&w=196&h=110");
                //else
                tempvideo.setResimurl(j_video.getString("resimUrl"));
                videohaberList.add(tempvideo);
                //Log.d("Haber:",temp.toString());

            }



        } catch (JSONException e) {
            e.printStackTrace();
            Log.d("Hata Sebebi:", e.toString());
        } catch (RuntimeException e) {
            Log.d("Hata Sebebi:", e.toString());
        }

        return null;
    }
    protected void onPostExecute(Void result) {
        super.onPostExecute(result);
        pDialog.dismiss();
        //haberListView.setAdapter(new haberAdapter());
        mergeAdapter = new MergeAdapter();
        mergeAdapter.addAdapter(new haberAdapter());
        mergeAdapter.addAdapter(new videobaslikAdapter());
        mergeAdapter.addAdapter(new  videoHaberAdapter());
        haberListView.setAdapter(mergeAdapter);
    }
}



private class haberAdapter extends ArrayAdapter<Haber> {

    public haberAdapter() {

        super(getActivity(), R.layout.kategorili_haber_item, haberList);

    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        View itemView = convertView;
        LayoutInflater inflater = ((Activity) getContext()).getLayoutInflater();
        if (itemView == null)
            itemView = inflater.inflate(R.layout.kategorili_haber_item, parent, false);

        final Haber currentHaber = haberList.get(position);
        TextView baslik = (TextView) itemView.findViewById(R.id.haberBaslik);
        baslik.setText(currentHaber.getBaslik().toUpperCase());
        ImageView resim = (ImageView) itemView.findViewById(R.id.haberResim);

        if(currentHaber.getResimurl().equals("null")){
            resim.setImageResource(R.drawable.ic_empty);
        }else{

            if(currentHaber.getVideo()==0) {
                //get image size and set
               // URL url = new URL(MainActivity.resimKucukUrl + currentHaber.getResimurl());
               // Bitmap bmp = BitmapFactory.decodeStream();
               // imageView.setImageBitmap(bmp);
                Picasso.with(getActivity())
                        .load(MainActivity.resimKucukUrl + currentHaber.getResimurl())
                        .resize(800, 0)//wiith and height

                        .into(resim);
            }else {

                Picasso.with(getActivity())
                        .load(currentHaber.getResimurl())
                        .into(resim);
            }
            Log.d("Haber Adapt:",currentHaber.toString());
        }

        itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if(currentHaber.getVideo()==1){
                    Intent myIntent = new Intent(getActivity(), videoHaberDetay.class);
                    Log.d("Kategorili haber","Seçilen video haber id:"+currentHaber.getId());
                    myIntent.putExtra("haberId",""+ currentHaber.getId());
                    startActivityForResult(myIntent, 0);
                }else{
                    Intent myIntent = new Intent(getActivity(), haber_detay.class);
                    Log.d("Kategorili haber","Seçilen haber id:"+currentHaber.getId());
                    myIntent.putExtra("haberId",""+ currentHaber.getId());
                    startActivityForResult(myIntent, 0);
                }

            }
        });


        return itemView;
    }
}
private class videobaslikAdapter extends ArrayAdapter<Haber> {
    public videobaslikAdapter() {

        super(getActivity(), R.layout.baslik_videohatti, basliklist);

    }
    public View getView(int position, View convertView, ViewGroup parent) {

        View itemView = convertView;
        LayoutInflater inflater = ((Activity) getContext()).getLayoutInflater();
        if (itemView == null)
            itemView = inflater.inflate(R.layout.baslik_videohatti, parent, false);
        return itemView;
    }
}
private class videoHaberAdapter extends ArrayAdapter<Haber> {

    public videoHaberAdapter() {

        super(getActivity(), R.layout.kategorili_haber_item, videohaberList);

    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        View itemView = convertView;
        LayoutInflater inflater = ((Activity) getContext()).getLayoutInflater();
        if (itemView == null)
            itemView = inflater.inflate(R.layout.kategorili_haber_item, parent, false);

        final Haber currentHaber = videohaberList.get(position);
        TextView baslik = (TextView) itemView.findViewById(R.id.haberBaslik);
        baslik.setText(currentHaber.getBaslik().toUpperCase());
        ImageView resim = (ImageView) itemView.findViewById(R.id.haberResim);

        if(currentHaber.getResimurl().equals("null")){
            resim.setImageResource(R.drawable.ic_empty);
        }else{

            if(currentHaber.getVideo()==0) {
                //get image size and set
                // URL url = new URL(MainActivity.resimKucukUrl + currentHaber.getResimurl());
                // Bitmap bmp = BitmapFactory.decodeStream();
                // imageView.setImageBitmap(bmp);
                Picasso.with(getActivity())
                        .load(MainActivity.resimKucukUrl + currentHaber.getResimurl())
                        .resize(800, 0)//wiith and height

                        .into(resim);
            }else {

                Picasso.with(getActivity())
                        .load(MainActivity.resimKucukUrl + currentHaber.getResimurl())
                        .resize(800, 0)//wiith and height
                        .into(resim);
            }
            Log.d("Haber Adapt:",currentHaber.toString());
        }

        itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if(currentHaber.getVideo()==1){
                    Intent myIntent = new Intent(getActivity(), videoHaberDetay.class);
                    Log.d("Kategorili haber","Seçilen video haber id:"+currentHaber.getId());
                    myIntent.putExtra("haberId",""+ currentHaber.getId());
                    startActivityForResult(myIntent, 0);
                }else{
                    Intent myIntent = new Intent(getActivity(), haber_detay.class);
                    Log.d("Kategorili haber","Seçilen haber id:"+currentHaber.getId());
                    myIntent.putExtra("haberId",""+ currentHaber.getId());
                    startActivityForResult(myIntent, 0);
                }

            }
        });


        return itemView;
    }
}
public class MergeAdapter extends BaseAdapter implements SectionIndexer {
    protected ArrayList<ListAdapter> pieces = new ArrayList<ListAdapter>();
    protected String noItemsText;



    public MergeAdapter() {
        super();
    }


    public void addAdapter(ListAdapter adapter) {
        pieces.add(adapter);
        adapter.registerDataSetObserver(new CascadeDataSetObserver());
    }

    public Object getItem(int position) {
        for (ListAdapter piece : pieces) {
            int size = piece.getCount();

            if (position < size) {
                return (piece.getItem(position));
            }

            position -= size;
        }

        return (null);
    }

    public void setNoItemsText(String text){
        noItemsText = text;
    }

    public ListAdapter getAdapter(int position) {
        for (ListAdapter piece : pieces) {
            int size = piece.getCount();

            if (position < size) {
                return (piece);
            }

            position -= size;
        }

        return (null);
    }

    public int getCount() {
        int total = 0;

        for (ListAdapter piece : pieces) {
            total += piece.getCount();
        }

        if(total == 0 && noItemsText != null){
            total = 1;
        }

        return (total);
    }



    @Override
    public int getViewTypeCount() {
        int total = 0;

        for (ListAdapter piece : pieces) {
            total += piece.getViewTypeCount();
        }

        return (Math.max(total, 1)); // needed for setListAdapter() before
        // content add'
    }


    @Override
    public int getItemViewType(int position) {
        int typeOffset = 0;
        int result = -1;

        for (ListAdapter piece : pieces) {
            int size = piece.getCount();

            if (position < size) {
                result = typeOffset + piece.getItemViewType(position);
                break;
            }

            position -= size;
            typeOffset += piece.getViewTypeCount();
        }

        return (result);
    }


    @Override
    public boolean areAllItemsEnabled() {
        return (false);
    }


    @Override
    public boolean isEnabled(int position) {
        for (ListAdapter piece : pieces) {
            int size = piece.getCount();

            if (position < size) {
                return (piece.isEnabled(position));
            }

            position -= size;
        }

        return (false);
    }


    public View getView(int position, View convertView, ViewGroup parent) {
        for (ListAdapter piece : pieces) {
            int size = piece.getCount();

            if (position < size) {

                return (piece.getView(position, convertView, parent));
            }

            position -= size;
        }

        if(noItemsText != null){
            TextView text = new TextView(parent.getContext());
            text.setText(noItemsText);
            return text;
        }

        return (null);
    }


    public long getItemId(int position) {
        for (ListAdapter piece : pieces) {
            int size = piece.getCount();

            if (position < size) {
                return (piece.getItemId(position));
            }

            position -= size;
        }

        return (-1);
    }

    public int getPositionForSection(int section) {
        int position = 0;

        for (ListAdapter piece : pieces) {
            if (piece instanceof SectionIndexer) {
                Object[] sections = ((SectionIndexer) piece).getSections();
                int numSections = 0;

                if (sections != null) {
                    numSections = sections.length;
                }

                if (section < numSections) {
                    return (position + ((SectionIndexer) piece)
                            .getPositionForSection(section));
                } else if (sections != null) {
                    section -= numSections;
                }
            }

            position += piece.getCount();
        }

        return (0);
    }

    public int getSectionForPosition(int position) {
        int section = 0;

        for (ListAdapter piece : pieces) {
            int size = piece.getCount();

            if (position < size) {
                if (piece instanceof SectionIndexer) {
                    return (section + ((SectionIndexer) piece)
                            .getSectionForPosition(position));
                }

                return (0);
            } else {
                if (piece instanceof SectionIndexer) {
                    Object[] sections = ((SectionIndexer) piece).getSections();

                    if (sections != null) {
                        section += sections.length;
                    }
                }
            }

            position -= size;
        }

        return (0);
    }

    public Object[] getSections() {
        ArrayList<Object> sections = new ArrayList<Object>();

        for (ListAdapter piece : pieces) {
            if (piece instanceof SectionIndexer) {
                Object[] curSections = ((SectionIndexer) piece).getSections();

                if (curSections != null) {
                    for (Object section : curSections) {
                        sections.add(section);
                    }
                }
            }
        }

        if (sections.size() == 0) {
            return (null);
        }

        return (sections.toArray(new Object[0]));
    }

    private class CascadeDataSetObserver extends DataSetObserver {
        @Override
        public void onChanged() {
            notifyDataSetChanged();
        }

        @Override
        public void onInvalidated() {
            notifyDataSetInvalidated();
        }
    }
}

1 个答案:

答案 0 :(得分:1)

使用两种不同的布局简单地添加两次.FooterView。