【问题标题】:How to create big dynamic view in android?如何在android中创建大动态视图?
【发布时间】:2017-07-05 16:53:45
【问题描述】:

我在多次创建和删除视图时遇到了一个大问题

例如,我在 sqlite 数据库中搜索并获取 200 条记录,我想在我的活动中创建 200 个视图,但它会在 3 或 4 秒内发生,这对用户体验和性能不利。如何增加每次创建视图的时间?

这些是我创建视图的代码

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();


       }


    }

请帮助我如何优化我的代码 谢谢。

【问题讨论】:

  • 你为什么不使用ListviewRecyclerView
  • @atef Hares 因为我想创建图书阅读器,listview 或 Recyclerview 对我没有用
  • 查看answer

标签: android performance android-layout android-view dynamic-view


【解决方案1】:

好吧,在 android 中有一个视图是为这个特定目的而制作的,比如 ListViewRecyclerView 等等,这些视图得到 Adapter 对象,适配器恢复数据(在你的情况下是你的 200 行)和ListView 例如只创建出现在屏幕上的项目,当用户向下滚动时,ListView 创建出现的新视图并删除旧视图,这给用户在巨大的项目列表中的奇妙用途,如果您必须使用自己的自定义视图,我建议您使用 ListViewAdapter,您可以从 ListView 扩展并以自己的方式实现。

you can read more here

如果您想要与 ListView 不同的特定 gui,或者您需要帮助,请告诉我

更新: 如果您想创建自己的 impl,您如何看待这个方向?

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();

}

在您的下一页或上一页按钮上

button.setOnClickListener(new View.OnClickListener {

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

})

【讨论】:

  • 我知道 listview 和适配器,但在这种情况下没用,因为我们想在书里扫一扫,但我只想知道如何缩短创建视图的时间。
  • 好主意,你已经有很好的视图,因为你知道创建视图和恢复视图是 android 设备的工作,你可以为 200 个视图做的最好的实现是你可以实现的同一个适配器实现您自己,但在此之前,我建议您使用AdapterViewFlipper,他非常易于使用且速度很快,您可以添加交换动画like here
  • 我使用了viewflipper,但每张幻灯片都会改变,现在我将测试您的解决方案
  • mayan anger 我检查了你的链接,这不是我需要的,因为我们在每个页面中没有相同的布局此外我们需要创建动态小部件,
  • mayan anger 我创建了这个类,我不知道如何在这个类中填充我的数据,因为我在这本书中有 604 页,每个页面都有自己的动态视图,另一方面当用户更改每个如何创建新视图时
猜你喜欢
  • 1970-01-01
  • 2022-11-28
  • 2013-07-17
  • 2011-02-12
  • 2017-10-02
  • 2015-05-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多