【问题标题】:android - smooth scrolling listview with data from URLandroid - 使用来自 URL 的数据平滑滚动列表视图
【发布时间】:2023-03-19 17:15:01
【问题描述】:

我的 listview 的 convertView 中有几个小部件,所有数据都是从 json 解析的。首先,我从分别解析的数据形成数组,然后在我的 ArrayAdapter 中处理这些最终数组。但是每次我滚动列表视图时,应用程序都会转到服务器以获取相应的信息(正如我在日志中看到的那样),尽管它应该已经存储在数组中。也许,这是我的 listView 滚动很慢的原因之一。有什么问题?

我的 ArrayAdapter 代码:

public class CustomAdapterSecond extends ArrayAdapter<String> {

private final Activity context;
private final String[] sSecondStartTimeArray;
private final String[] sSecondEndTimeArray;
private final String[] sSecondTitleArray;
public CustomAdapterSecond(Activity context,
                     String[] sSecondStartTimeArray, String[] sSecondEndTimeArray, String[] sSecondTitleArray) {
    super(context, R.layout.relcell2, sSecondStartTimeArray);
    this.context = context;
    this.sSecondStartTimeArray = sSecondStartTimeArray;
    this.sSecondEndTimeArray = sSecondEndTimeArray;
    this.sSecondTitleArray = sSecondTitleArray;
}

static class ViewHolder {
    public TextView textStartTime;
    public TextView textEndTime;
    public TextView textUpper;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    String AllData2 = HTTPRequest.GetData(URL);
    ViewHolder holder;
    if (convertView == null) {
        LayoutInflater inflater = context.getLayoutInflater();
        convertView = inflater.inflate(R.layout.relcell2main,parent,false);
        holder = new ViewHolder();
        holder.textStartTime = (TextView)convertView.findViewById(R.id.textStartTime);
        holder.textEndTime = (TextView)convertView.findViewById(R.id.textEndTime);
        holder.textUpper = (TextView)convertView.findViewById(R.id.textUpper);
        convertView.setTag(holder);
    }
    else {
        holder = (ViewHolder)convertView.getTag();
    }
    holder.textStartTime.setText(sSecondStartTimeArray[position]);
    holder.textEndTime.setText(sSecondEndTimeArray[position]);
    holder.textUpper.setText(sSecondTitleArray[position]);

    final int chosen = position;
    convertView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            int chosenP = chosen;
            if (chosenP != 0) {
                Intent intent = new Intent(context, MainThird.class);
                intent.putExtra("chosenItem", chosenP);
                context.startActivity(intent);                   
            }
        }
    });
    return convertView;
}

以及我在其中形成数组的 Activity 的代码:

public class MainSecond extends AppCompatActivity {

ListView listViewLeft;
ListView listViewRight;
int secondChosen=-1;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.mainsecond);

    secondChosen = getIntent().getExtras().getInt("chosenItem");
    final String sStartDate = getIntent().getExtras().getString("startDate");
    String sEndDate = getIntent().getExtras().getString("endDate");
    final String AllData1 = HTTPRequest.GetData(URL1);

    TabHost tabs = (TabHost) findViewById(android.R.id.tabhost);
    tabs.setup();
    TabHost.TabSpec spec = tabs.newTabSpec("tag1");
    spec.setContent(R.id.tab1);
    spec.setIndicator("Caption1");
    tabs.addTab(spec);
    spec = tabs.newTabSpec("tag2");
    spec.setContent(R.id.tab2);
    spec.setIndicator("Caption2");
    tabs.addTab(spec);
    tabs.setCurrentTab(0);

    final String AllData2 = HTTPRequest.GetData(URL);

    TabHost tabs2 = (TabHost) findViewById(R.id.tabhost2);
    tabs2.setup();
    TabHost.TabSpec spec2 = tabs2.newTabSpec("tag12");
    spec2.setContent(R.id.tab12);
    spec2.setIndicator(sStartDate);
    tabs2.addTab(spec2);
    if (!(sStartDate.equals(sEndDate))) {
        spec2 = tabs2.newTabSpec("tag22");
        spec2.setContent(R.id.tab22);
        spec2.setIndicator(sEndDate);
        tabs2.addTab(spec2);
        tabs2.setCurrentTab(0);
        listViewRight = (ListView)findViewById(R.id.listViewRight);
    }

    listViewLeft = (ListView)findViewById(R.id.listViewLeft);

    //TODO:CreateListView
    int leng = HTTPRequest.GetLength(AllData2);
    int lengLeft=0;
    for (int i=0;i<leng;i++) {
        if (HTTPRequestSecond.GetSecondStartFecha(AllData2,i).equals(sStartDate)) lengLeft++;
    }
    ArrayList<String>sSecondStartTime=new ArrayList<>();
    ArrayList<String>sSecondEndTime=new ArrayList<>();
    ArrayList<String>sSecondTitle=new ArrayList<>();
    for (int j=0;j<lengLeft;j++) {
        sSecondStartTime.add(j,HTTPRequestSecond.GetSecondStartTime(AllData2,j));
        sSecondEndTime.add(j,HTTPRequestSecond.GetSecondEndTime(AllData2, j));
        sSecondTitle.add(j,HTTPRequestSecond.GetSecondTitle(AllData2, j));
    }
    String[] sSecondStartTimeArra = new String[sSecondStartTime.size()];
    sSecondStartTimeArra = sSecondStartTime.toArray(sSecondStartTimeArra);
    String[] sSecondEndTimeArra = new String[sSecondEndTime.size()];
    sSecondEndTimeArra = sSecondEndTime.toArray(sSecondEndTimeArra);
    String[] sSecondTitleArra = new String[sSecondTitle.size()];
    sSecondTitleArra = sSecondTitle.toArray(sSecondTitleArra);
    final String[] sSecondStartTimeArray = sSecondStartTimeArra;
    final String[] sSecondEndTimeArray = sSecondEndTimeArra;
    final String[] sSecondTitleArray = sSecondTitleArra;
    CustomAdapterSecond adapter2 = new
            CustomAdapterSecond(MainSecond.this,sSecondStartTimeArray,sSecondEndTimeArray,sSecondTitleArray);
    listViewLeft.setAdapter(adapter2);
}

最后我搞砸了,但我只是想让数组成为最终的(认为在每次滚动期间不使用 Internet 会有所帮助)所以这就是它看起来如此愚蠢的原因:(

【问题讨论】:

    标签: android json listview android-arrayadapter smooth-scrolling


    【解决方案1】:

    在您的适配器getView() 中,第一行再次调用远程服务。评论并重试

    public View getView(int position, View convertView, ViewGroup parent) {
        // String AllData2 = HTTPRequest.GetData(URL);
    ...
    

    【讨论】:

    • 它有帮助,谢谢!但是,如果我有另一个带有字符串和图像的列表视图(也从 URL 解析),有时,当我运行该 Activity 并在运行后立即滚动它(无需等待几秒钟)时,它会从开头开始创建列表视图从第一个不可见项目开始的数组?例如,如果我有 4 个 listView 项目,其中 3 个在开始屏幕上可见,如果我一次滚动它,第 4 个看起来就像第 1 个。怎么办?
    • @JustinMcGuire 通常我所做的是任何RecyclerView/ ListVIew 我有3个强制性单元格布局,我在任何常规单元格之上用于不同情况:1)loading_cell.xml - 显示数据更新正在进行时的一些加载状态 2) empty_cell.xml - 当新数据为空或空时显示,即没有显示 3) error_cell.xml - 当新数据出错时。当您下载数据时,请显示 (1),当数据准备好时,使用新数据创建新适配器,然后将其设置为 ListView.setAdapter()Note 从不getView() 中添加繁重的远程调用或仅在绝对必要时添加。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多