在Android中复制Apple的搜索

时间:2011-09-15 12:49:35

标签: android android-layout

我想创建一个类似于此处显示的用户界面http://appsreviews.com/wp-content/uploads/2010/08/Cures-A-Z-App-for-iPhone.jpg

我开始尝试将两个自定义列表并排放置,就像在此代码中一样

import java.util.ArrayList;
import java.util.List;
import java.util.WeakHashMap;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;



public class Emp extends Activity {

    /** Called when the activity is first created. */

    private String tableName = DBHelper.tableName;

    private SQLiteDatabase newDB;


public static WeakHashMap<String, Empbook> temp = new WeakHashMap<String, Empbook>();

final List<Empbook> listOfEmpbook = new ArrayList<Empbook>();
final List<String> listOfAlphabets = new ArrayList<String>();
TextView txt;
EmpbookAdapter adapter = new EmpbookAdapter(this, listOfEmpbook);

Integer pos;
Integer count=0;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    txt=(TextView)findViewById(R.id.textView1);
    ListView list = (ListView) findViewById(R.id.ListView01);
    ListView alist = (ListView) findViewById(R.id.ListView02);

    list.setClickable(true);
    alist.setClickable(true);
    AlphabetListAdapter alphabetadapter = new AlphabetListAdapter(this,
            listOfAlphabets);
    list.setAdapter(adapter);
            alist.setAdapter(alphabetadapter);

alphabetadapter用于显示屏幕右侧字母的列表。 我的XML如下:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal" android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <TextView android:text="TextView" android:id="@+id/textView1"
        android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
    <ListView android:id="@+id/ListView01" android:layout_width="280dp"
        android:orientation="vertical" android:layout_height="wrap_content"></ListView>
    <ListView android:id="@+id/ListView02" android:layout_width="wrap_content"
        android:orientation="vertical" android:paddingLeft="282dp"
        android:layout_height="wrap_content"></ListView>
</LinearLayout>

发生的问题是,一次只能显示一个视图(上一个xml中显示的视图,而另一个视图不显示)。

有人可以把我推向正确的方向吗?

编辑:我试图设置列表的权重设置为0并将另一个设置为1,它现在部分工作我可以看到两个列表但是其中一个列表没有被填充....将更新如果我解决了。

在下面发布了一个答案(尽管有一个列表视图丢失了。)检查出来。

6 个答案:

答案 0 :(得分:1)

如果旁边的索引是你想要的,你应该试试这个:http://hello-android.blogspot.com/2010/11/sideindex-for-android.html

答案 1 :(得分:1)

如果要并排添加元素,这些元素一起填充其父元素:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal" 
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <ListView
      android:id="@+id/ListView01"
      android:layout_width="0dp"
      android:layout_height="match_parent"
      android:layout_weight="90"
      android:background="#FFFF0000"/>    
    <ListView
      android:id="@+id/ListView02"
      android:layout_width="0dp"
      android:layout_height="match_parent"
      android:layout_weight="10"
      android:background="#FF00FF00"/>
</LinearLayout>

你有“wrap_parent”作为第二个元素的高度。如果没有正确填充,它的高度为0 - 我已将其更改为与父级匹配。我还添加了一个使用“百分比”填充的系统。

此外,所有其他“fill_parent”标签我已更改为“match_parent” - 不是因为它更改了代码的功能,而是因为“fill_parent”已弃用,因为作为标签会产生误导。

另外,我添加了一些背景元素,可以更有效地调试问题所在。

我还建议你应该瞄准的是一个View(不是ListView,即使我保留了这个例子),它将放在另一个之上(正如Apple搜索有他们的字母):

<RelativeLayout  xmlns:android="http://schemas.android.com/apk/res/android"                 
                android:layout_width="match_parent"
                android:layout_height="match_parent">
    <ListView android:id="@+id/ListView01"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:background="#FFFF0000"/>    
    <!-- Since the contents of the view don't change it seems wasteful to create this as a listview -->
    <ListView android:id="@+id/ListView02"
              android:layout_width="20dp"
              android:layout_height="match_parent"
              android:background="#FF00FF00"
              android:layout_alignParentRight="true"
              android:layout_marginRight="5dp"/>
</RelativeLayout>

答案 2 :(得分:1)

现在找到一个解决方法,使用textview和嵌套在framelayout中的listview,如下所示:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent" android:layout_height="fill_parent">
    <LinearLayout android:orientation="vertical"
        android:layout_width="fill_parent" android:layout_height="fill_parent">

        <ListView android:id="@+id/ListView01" android:layout_width="fill_parent"
            android:layout_height="fill_parent" android:cacheColorHint="#00000000" />
        <TextView android:id="@android:id/empty"
            android:layout_width="fill_parent" android:layout_height="fill_parent"
            android:text="textview" />

    </LinearLayout>

    <LinearLayout android:orientation="vertical"
        android:background="@android:color/transparent" android:id="@+id/sideIndex"
        android:paddingLeft="280dip"
        android:layout_width="300dip" android:layout_height="fill_parent"
        android:gravity="center_horizontal">
        <TextView android:text="T" android:id="@+id/textView1"
            android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>    
    </LinearLayout>
    </FrameLayout>

更多信息可以在http://dotslasha.in/wp/143/creating-floating-views-in-android找到。 Ty和干杯! :)

答案 3 :(得分:0)

您无需亲自实现此功能,Google已帮助您使用API​​来使用其搜索功能。

关于这个问题的文档应该足以让你从头到尾。它可以在这里找到:http://developer.android.com/guide/topics/search/search-dialog.html

答案 4 :(得分:0)

在第二个ListView中,您有一个填充:android:paddingLeft="282dp"。我假设你不是仅在横向编码平板电脑,所以只需删除padding属性。

答案 5 :(得分:0)

删除第一个元素的文本视图(可以使用addHeaderView()替换它,或者将此linearlayout包装在垂直元素上)。

仔细查看如何在以下代码中设置宽度和高度:

   <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="horizontal" android:layout_width="match_parent"
        android:layout_height="match_parent">
        <ListView
          android:id="@+id/ListView01"
          android:orientation="vertical"
          android:layout_width="wrap_content"
          android:layout_height="match_parent"
          android:layout_weight="1"
        ></ListView>
        <ListView
          android:id="@+id/ListView02"
          android:orientation="vertical"
          android:layout_width="wrap_content"
          android:layout_height="match_parent"
          android:layout_weight="0"
        ></ListView>
    </LinearLayout>

根据我的经验,如果宽度设置为wrap_content,则权重才能正常工作。