【问题标题】:Unable to scroll the View in android无法在android中滚动视图
【发布时间】:2014-01-03 08:23:24
【问题描述】:

我有两个 ListView,我必须从那里选择两个项目。然后我的安卓应用中会出现一个新窗口。

我使用了 ScrollView,但无法滚动画布。 我的画布很大,这就是我需要滚动的原因。

需要帮助!提前致谢。

布局

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent"
   android:paddingLeft="16dp"
   android:paddingRight="16dp" >

   <ListView
       android:id="@+id/list1"
       android:layout_width="60dp"
       android:layout_height="fill_parent"
       android:layout_alignParentLeft="true" />

   <ListView
       android:id="@+id/list2"
       android:layout_width="60dp"
       android:layout_height="fill_parent"
       android:layout_alignParentRight="true" />

   <ScrollView
        android:id="@+id/mapview1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
   />

</RelativeLayout>

活动

package com.example.tester1;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ScrollView;
import android.widget.ListView;
import android.widget.TextView;

public class MainActivity extends Activity {
    Map map;
    String Source = "Blank";
    String Destination = "Blank";
    ListView list1;
    ListView list2;
    String [] listArray;
    BufferedReader AirportLocation;


    @Override
    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        try {
            AirportLocation = new BufferedReader(
                    new InputStreamReader(getAssets().open("Airport-Location3.txt")));
        } catch (IOException e) {
            e.printStackTrace();
        }
        listArray = new String[1168];

        String line1;
        int c = 0;// location detector
        try {
            while((line1 = AirportLocation.readLine())!=null){
                StringTokenizer s = new StringTokenizer(line1);
                String airport = s.nextToken();
                listArray[c] = airport;
                c++;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }


        list1 = (ListView) findViewById(R.id.list1);
        list2 = (ListView) findViewById(R.id.list2);
        ArrayAdapter<String> adapter1 = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,listArray);
        ArrayAdapter<String> adapter2 = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,listArray);
        list1.setAdapter(adapter1);
        list2.setAdapter(adapter2);
        list1.setOnItemClickListener(new SourceSelection());
        list2.setOnItemClickListener(new DestinationSelection());

    }

    public void Draw(){
        map = new Map(this);
        setContentView(map);
    } 

class SourceSelection implements AdapterView.OnItemClickListener{

        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
            TextView temp = (TextView) arg1;
            Source = temp.getText()+"";
            if(!Source.equals("Blank") && !Destination.equals("Blank")){
                Draw();
            }
        }
    }

    class DestinationSelection implements AdapterView.OnItemClickListener{

        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
            TextView temp = (TextView) arg1;
            Destination = temp.getText()+"";
            if(!Source.equals("Blank") && !Destination.equals("Blank")){
                Draw();
            }
        }
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
    public class Map extends ScrollView {
        Paint paint = new Paint();
        public Map(Context context) {
            super(context);
        }
        @Override
        public void onDraw(Canvas canvas) {
            paint.setColor(Color.RED);
            canvas.drawCircle(100, 100, 20, paint);
            canvas.drawLine(2000, 2000, 5000, 5000, paint);
        }
        @Override
        protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
            // Compute the height required to render the view
            // Assume Width will always be MATCH_PARENT.
            int width = MeasureSpec.getSize(widthMeasureSpec);
            int height = 3000 + 50; // Since 3000 is bottom of last Rect to be drawn added and 50 for padding.
            setMeasuredDimension(width, height);
        }
    }
}

【问题讨论】:

  • 这不是一个确切的答案,而是与设计有关。看来您需要dropdown 而不是listview。试试developer.android.com/guide/topics/ui/controls/spinner.html
  • 谢谢先生,但我的问题出在 ScrollView.. :(
  • 你能告诉我我应该怎么做才能滚动画布.. :(
  • 我认为列表视图不需要任何滚动视图。它们本身是可滚动的。
  • 是的 ListView 本身可滚动,但我想滚动画布:(

标签: android android-layout android-canvas android-view android-scrollview


【解决方案1】:

使用

<ScrollView
        android:id="@+id/mapview1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
   />
   <ListView
       android:id="@+id/list2"
       android:layout_width="60dp"
       android:layout_height="fill_parent"
       android:layout_alignParentRight="true" />
</ScrollView>

【讨论】:

  • 你不应该把listView放在scrollView里面,把scrollView放在listView里面。谷歌安卓开发团队不推荐。这可能会导致不同的意外 UI 包。
【解决方案2】:

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent">

   <RelativeLayout
       android:layout_width="fill_parent"
       android:layout_height="fill_parent"
       android:paddingLeft="16dp"
       android:paddingRight="16dp" />

   <ListView
       android:id="@+id/list1"
       android:layout_width="60dp"
       android:layout_height="fill_parent"
       android:layout_alignParentLeft="true" />

   <ListView
       android:id="@+id/list2"
       android:layout_width="60dp"
       android:layout_height="fill_parent"
       android:layout_alignParentRight="true" />

   </RelativeLayout>

</ScrollView>

这是您正在寻找的,还是我没有理解您的询问?

编辑:根据另一个答案的评论,顶部是错误的。 至于 OP 想要什么,ExpandableListView 可以工作吗? here 是一个关于它的教程。

【讨论】:

  • 不,我想在从列表视图中选择项目后使画布可滚动,画布将打开并且画布应该是可滚动的...
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-11-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多