无法在Android中滚动视图

时间:2014-01-03 08:23:24

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

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

我使用了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);
        }
    }
}

2 个答案:

答案 0 :(得分:0)

使用

<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>

答案 1 :(得分:0)

<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是关于此的教程。