0

i want to make an app as you can see a photo app web version

to do this news shower app, there is listview items for normal news and video news items after an video title i have created a listview to do this and added two adapter to this listview. one of adapter for normal news and second adapter for video news. But i need to add button shown in picture as "Daha fazla haber" this button to add more news for normal news and i also need second more news button under video news listview items. So if users click more news button then app will show more news after normal news items before video news items. how to do this? is it possible to make in same listview?

my xml is like that

<?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>

my adapter is like that

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();
        }
    }
}
  • Try : http://stackoverflow.com/questions/3227182/android-adding-footer-to-listview-addfooterview to add footer in listview – avinash Dec 21 '16 at 08:45
  • i have already seen this link but i need to learn how to add two footer to one listview. one of them is to add items to middle of other items and second pf them will add items to end of other items.. is it possible @avinash – Zehra Duman Küçükkoç Dec 21 '16 at 08:49
  • If this is the case then you need a section header listview. where you can add headers in middle of listview. Try recyclerview where u can render view on view type. Try : https://developer.android.com/training/material/lists-cards.html – avinash Dec 21 '16 at 08:55
  • thanks a lot @avinash i found what i want eventually http://stackoverflow.com/questions/26585941/recyclerview-header-and-footer – Zehra Duman Küçükkoç Dec 22 '16 at 08:28
  • @avinash your advice is perfect about using recyclerview – Zehra Duman Küçükkoç Dec 22 '16 at 08:30

1 Answers1

1

Simply addFooterView two times with your two different layouts.

Parin Parikh
  • 385
  • 1
  • 6
  • how to two different layout? if i put two listview to same layout then each listview is goning to have own scroll how to use two list view veritacally same layout?. Actually you said that two different layout... so... should i create two different layout and put them another layout inside dynamically? – Zehra Duman Küçükkoç Dec 21 '16 at 11:37