gpt4 book ai didi

android - 如何在 Android 中制作 Instagram ListView ?

转载 作者:太空狗 更新时间:2023-10-29 12:46:00 26 4
gpt4 key购买 nike

我想让 listview 和 instagram 一样...(分段)

我在谷歌中搜索并找到了一些示例,但它无法正常工作。这是我搜索的链接:Link 1Link 2 .但是在第一个链接中,我找不到任何类似 instagram 的 ListView 的解决方案。

至于第二个链接,我制作了一个演示并运行它并且运行良好。唯一的问题是,当我在 header 中放置两个 TextView 时,它不起作用。

如果你能在这方面帮助我,那对我来说会很棒..

这个来自 Second Link 的演示..

package com.example.Section_Listview;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class InstaHeaderActivity extends Activity implements AbsListView.OnScrollListener{

ListView list;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
list = (ListView) findViewById(R.id.list);
list.setAdapter(new Adapter(this));
list.setOnScrollListener(this);
}

public void onScroll(AbsListView view, int firstVisibleItem,int visibleItemCount, int totalItemCount)
{
//the listview has only few children (of course according to the height of each child) who are visible
for(int i=0; i < list.getChildCount(); i++)
{
View child = list.getChildAt(i);
ViewHolder holder = (ViewHolder) child.getTag();

//if the view is the first item at the top we will do some processing
if(i == 0)
{
boolean isAtBottom = child.getHeight() <= holder.header.getBottom();
int offset = holder.previousTop - child.getTop();
if(!(isAtBottom && offset > 0))
{
holder.previousTop = child.getTop();
holder.header.offsetTopAndBottom(offset);
holder.header.invalidate();
}
} //if the view is not the first item it "may" need some correction because of view re-use
else if (holder.header.getTop() != 0)
{
int offset = -1 * holder.header.getTop();
holder.header.offsetTopAndBottom(offset);
holder.previousTop = 0;
holder.header.invalidate();
}
}
}

public void onScrollStateChanged(AbsListView view, int scrollState) {}

private static class Adapter extends ArrayAdapter<String> {
public Adapter(Context context) {
super(context, R.layout.row, R.id.header);
for(int i=0; i < 50; i++){
add(Integer.toString(i));
}
}

@Override
public View getView(int position, View convertView, ViewGroup parent)
{
if(convertView == null)
{
convertView = LayoutInflater.from(getContext()).inflate(R.layout.row, parent, false);
ViewHolder holder=new ViewHolder();
holder.header = (TextView) convertView.findViewById(R.id.header);
convertView.setTag(holder);
}
ViewHolder holder = (ViewHolder) convertView.getTag();
holder.header.setText(getItem(position));
return convertView;
}
}



public static class ViewHolder
{
TextView header;
int previousTop = 0;
}
}

主.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<ListView
android:id="@+id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
</ListView>
</LinearLayout>

child_header.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<TextView
android:id="@+id/childHeader"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="2dp"
android:text="childTest" />

</LinearLayout>

行.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" android:background="#FFFFFF">

<ListView
android:id="@+id/childList"
android:layout_width="fill_parent"
android:layout_height="150dp"
android:layout_alignParentLeft="true"
android:layout_below="@+id/header"
android:layout_marginTop="16dp" >
</ListView>


<TextView
android:id="@+id/header"
android:layout_width="fill_parent"
android:layout_height="40dp"
android:padding="12dp"
android:text="Deneme Row"
android:textColor="#ffffff"
android:background="#000000"/>

</RelativeLayout>

最佳答案

首先,你不能在一个文件中有两个公共(public)类,所以移动

public class ViewHolder {
TextView header;
int previousTop = 0;
}

到一个单独的文件。当你这样做时,删除 static 修饰符,这是不允许的。

要在标题行中显示第二个 TextView,您必须在 row.xml 中声明它:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#FFFFFF"
android:orientation="vertical" >

<ListView
android:id="@+id/childList"
android:layout_width="fill_parent"
android:layout_height="150dp"
android:layout_alignParentLeft="true"
android:layout_below="@+id/header"
android:layout_marginTop="16dp" >
</ListView>

<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >

<TextView
android:id="@+id/header"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:background="#000000"
android:padding="12dp"
android:textColor="#ffffff" />

<TextView
android:id="@+id/header2"
android:layout_width="fill_parent"
android:layout_height="40dp"
android:background="#000000"
android:padding="12dp"
android:text="second textview"
android:textColor="#ffffff" />
</LinearLayout>
</RelativeLayout>

修改后的 row.xml 将为您提供如下布局: enter image description here

要设置第二个 TextView 的内容,将您的 ViewHolder 类更改为:

public class ViewHolder {
TextView header;
TextView header2;
int previousTop = 0;
}

并将 Activity 中的 getView 方法更改为:

    @Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = LayoutInflater.from(getContext()).inflate(R.layout.row, parent, false);
ViewHolder holder = new ViewHolder();
holder.header = (TextView) convertView.findViewById(R.id.header);
holder.header2 = (TextView) convertView.findViewById(R.id.header2);
convertView.setTag(holder);
}
ViewHolder holder = (ViewHolder) convertView.getTag();
holder.header.setText(getItem(position));
holder.header2.setText("whatever you want");
return convertView;
}

关于android - 如何在 Android 中制作 Instagram ListView ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18184162/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com