2

i have a big problem with creating and removing views in many times

For example i searched in sqlite database and fetching 200 records, and i want create 200view in my activity but it happen in 3 or 4 seconds and it is bad for user experiences and performances.how to increase time for creating views every time?

These are my codes for creating views

public class CreateView {

    boolean header_flag=false;
    boolean first_widget=false;
    LinearLayout header_layout;

    List<Words_taha> words_tahaList=new ArrayList<>();




    public   void createHeader(Context context, LinearLayout main_layout, Words_taha words){



        if(header_flag==false){
            header_layout  =new LinearLayout(context);
            header_layout=new LinearLayout(context);
            header_layout.setOrientation(LinearLayout.HORIZONTAL);
            header_layout.setBackgroundResource(R.mipmap.sure_template);
            header_layout.setId(words.getW_id());

            header_layout.setGravity(Gravity.CENTER);

            main_layout.addView(header_layout);



            header_flag=true;

            words_tahaList.add(words);

        }
        else {

            words_tahaList.add(words);

            Collections.reverse(words_tahaList);


            for(int i=0;words_tahaList.size()>i;i++){

                TextView textView=new TextView(context);
               textView.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
                       LinearLayout.LayoutParams.WRAP_CONTENT));

                textView.setText(words_tahaList.get(i).getW_text()+" ");
                textView.setTag(words_tahaList.get(i).getW_id());
                Typeface typeface=Typeface.createFromAsset(context.getAssets(),"fonts/arabicNeirizi.ttf");
                textView.setTypeface(typeface);
                textView.setTextColor(Color.parseColor("#000000"));

                header_layout.addView(textView);
            }

            words_tahaList.clear();
            header_flag=false;

        }



    }


   public void createLabelForMainWordsInOneLine(Activity context, LinearLayout main_layout, List<Words_taha> words_tahaList, int count ){


        LinearLayout linearLayout=new LinearLayout(context);

        linearLayout.setOrientation(LinearLayout.HORIZONTAL);
       linearLayout.setGravity(Gravity.CENTER);


       if(words_tahaList.size()>0) {


           main_layout.addView(linearLayout);
           Collections.reverse(words_tahaList);

           for (Words_taha w : words_tahaList) {

               DroidTextView textView = new DroidTextView(context);

               textView.setText(w.getW_text());
               textView.setTag(w.getW_id());
               textView.setTextColor(Color.parseColor("#000000"));


               if (w.getW_type() == 3) {

                   textView.setLayoutParams(new LinearLayout.LayoutParams(130, 130));
                   textView.setGravity(Gravity.CENTER);
                   textView.setBackgroundResource(R.mipmap.sore);
               } else {
                  textView.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
                         LinearLayout.LayoutParams.WRAP_CONTENT));



               }


               linearLayout.addView(textView);


           }

           words_tahaList.clear();


       }


    }

Please help me how to optimization my code Thanks.

sr_ farzad
  • 45
  • 1
  • 9

1 Answers1

2

Well there are a views in android that made for this specific purpose, like ListView or RecyclerView and more, those views get Adapter object, the Adapter restore the data (in your case your 200 rows) and ListView For Example create only the Items That apear in the screen, And when the user scroll up ro down, ListView create the new Views that apear and delete the olds, this give to the user a fantastic use in huge lists of items, I recommend to lern use with ListView and Adapter if you must use your own custom View you can extends from ListView and implemnt in your own way.

you can read more here

if your want specific gui that diffrent from ListView or you need help let me know

Update : if you want to create your own impl what do you think about this direction ?

public abstract class CustomBookView extends LinearLayout implements CustomBookListener {

private int pageIndex = 0;
private List<WordsTasa> wordsList;

public CustomBookView(Context context, int pageIndex, List<WordsTasa> wordsList) {
    super(context);
    this.pageIndex = pageIndex;
    this.wordsList = wordsList;
}

public abstract View createPage(WordsTasa wordsTasa);

@Override
public void goNextPage() {
    if(wordsList.size()>=pageIndex+1)
        return;

    this.removeAllViews();
    //add your animation
    this.addView(createPage(wordsList.get(++pageIndex)));
}

@Override
public void goPreviousPage() {
    if(0<pageIndex-1)
        return;

    this.removeAllViews();
    //add your animation
    this.addView(createPage(wordsList.get(--pageIndex)));
}

public int getPageIndex() {
    return pageIndex;
}

public void setPageIndex(int pageIndex) {
    this.pageIndex = pageIndex;
}

public List<WordsTasa> getWordsList() {
    return wordsList;
}

public void setWordsList(List<WordsTasa> wordsList) {
    this.wordsList = wordsList;
}

public CustomBookView(Context context, AttributeSet attrs) {
    super(context, attrs);
}

public CustomBookView(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
}


public static class WordsTasa {
    private String words;

    public WordsTasa(String words) {
        this.words = words;
    }

    public String getWords() {
        return words;
    }

    public void setWords(String words) {
        this.words = words;
    }
}

}

 public interface CustomBookListener {

void goNextPage();
void goPreviousPage();

}

on your next page button or previous

button.setOnClickListener(new View.OnClickListener {

public void onClick(View v) {
    customBookListener.goNextPage();
}

})
mayan anger
  • 2,322
  • 2
  • 11
  • 19
  • 2
    i know listview and adapters but in this case not useful, because we want having swepping in book,but just i want to know how to improve time for creating views. – sr_ farzad Feb 16 '17 at 14:34
  • good idea and you aleady have nice views, as you know create views and restore views is havy work to android device, the best implement you can do for 200 views is the same adapter implement that you can implement yourself, but before that ,I recommend for you use the `AdapterViewFlipper` he is very easy to use and fast and you can add a swapping animation [like here](http://abhiandroid.com/ui/adapterviewflipper) – mayan anger Feb 16 '17 at 15:02
  • 1
    i used viewflipper but it changed by each slide,now i will test your solution – sr_ farzad Feb 16 '17 at 15:15
  • 1
    mayan anger i check your link,it is not my needing because we didnt have the same layout in every page furthermore we need to creating dynamically widgets, – sr_ farzad Feb 16 '17 at 15:21
  • mayan anger i created this class and i don't know how to fill my data in this class,because i have 604 page in this book and each page has own dynamic view, and in the other hand when users change each how to create new views – sr_ farzad Feb 16 '17 at 16:30
  • you are use the methods `createLabelForMainWordsInOneLine` and `createHeader` or something else to create your pages ? and if something else plase show me – mayan anger Feb 16 '17 at 16:48
  • how can i send my activity for you? – sr_ farzad Feb 16 '17 at 16:59
  • you can do winrar to me angermayan@gmail.com or send me gitup link – mayan anger Feb 16 '17 at 17:29