如何在 android studio 的弹出窗口内添加滚动的 listView?

Posted

技术标签:

【中文标题】如何在 android studio 的弹出窗口内添加滚动的 listView?【英文标题】:How to add listView with scroll inside a Popup window in android studio? 【发布时间】:2019-01-14 19:46:46 【问题描述】:

我正在尝试制作一个 android 应用程序,当单击按钮时会打开一个弹出窗口。并且,弹出窗口有四个带标题的 TextView 和 4 个带选项的 listView。但是,我无法在弹出窗口中添加 listView。单击按钮时会打开弹出窗口,但是当我添加 listView 及其适配器并尝试运行它时,应用程序会崩溃。我希望 PopUp 窗口的布局与 news.xml 文件中的布局相同。

activity_main.xml

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_
    android:layout_
    android:fillViewport="true"
    android:visibility="visible"
    tools:context=".MainActivity">


    <LinearLayout
        android:layout_
        android:layout_
        android:orientation="vertical"
        android:id="@+id/linear">

        <RelativeLayout
            android:layout_
            android:layout_>

            <Button
                android:id="@+id/news1"
                android:layout_
                android:layout_

                android:layout_alignParentStart="true"
                android:layout_marginStart="15dp"
                android:text="@string/news" />


        </RelativeLayout>

    </LinearLayout>

</ScrollView>

MainActivity.java

package com.example.abina.popup;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.Random;

import static android.view.Gravity.NO_GRAVITY;

public class MainActivity extends AppCompatActivity  

    private Button news1;
    private PopupWindow popupWindow;
    private LayoutInflater layoutInflater;
    private LinearLayout linearLayout;
    ListView lst;
    String [] local = "asdf","Sgadf","adfhtr","trdbfa";
    @RequiresApi(api = Build.VERSION_CODES.O)
    @Override
    protected void onCreate(Bundle savedInstanceState) 
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //for listView 1
        lst = (ListView) findViewById(R.id.listView1);
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,local);
        lst.setAdapter(adapter);
        lst.setOnItemClickListener(new AdapterView.OnItemClickListener() 
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) 
                Toast.makeText(MainActivity.this,local[i],Toast.LENGTH_SHORT).show();
            
        );

        linearLayout  = (LinearLayout) findViewById(R.id.linear);
        news1 = (Button) findViewById(R.id.news1);
        news1.setOnClickListener(new View.OnClickListener() 
            @Override
            public void onClick(View view) 
                layoutInflater = (LayoutInflater) getApplication().getSystemService(LAYOUT_INFLATER_SERVICE);
                ViewGroup container  = (ViewGroup) layoutInflater.inflate(R.layout.news,null);

                popupWindow = new PopupWindow(container,800,1300,true);
                popupWindow.showAsDropDown(news1);
                container.setOnTouchListener(new View.OnTouchListener() 
                    @Override
                    public boolean onTouch(View view, MotionEvent motionEvent) 
                        popupWindow.dismiss();
                        return false;
                    
                );
            
        );

    

news.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_
    android:layout_
    android:background="#ABEBC6">

    <RelativeLayout
        android:layout_
        android:layout_
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true">

        <ListView
            android:id="@+id/listView1"
            android:layout_
            android:layout_
            android:layout_alignParentStart="true"
            android:layout_below="@+id/textView"
            android:layout_marginStart="13dp" />

        <TextView
            android:id="@+id/textView"
            android:layout_
            android:layout_
            android:layout_alignParentStart="true"
            android:layout_alignParentTop="true"
            android:layout_marginStart="30dp"
            android:layout_marginTop="20dp"
            android:text="@string/local" />

        <TextView
            android:id="@+id/textView2"
            android:layout_
            android:layout_
            android:layout_alignParentEnd="true"
            android:layout_alignTop="@+id/textView"
            android:layout_marginEnd="30dp"
            android:text="@string/national" />

        <ListView
            android:id="@+id/listView2"
            android:layout_
            android:layout_
            android:layout_alignParentEnd="true"
            android:layout_alignTop="@+id/listView1" />

    </RelativeLayout>

    <RelativeLayout
        android:layout_
        android:layout_
        android:layout_marginTop="260dp">

        <TextView
            android:id="@+id/textView3"
            android:layout_
            android:layout_
            android:layout_alignParentStart="true"
            android:layout_alignParentTop="true"
            android:layout_marginStart="30dp"
            android:layout_marginTop="20dp"
            android:text="@string/sports1" />

        <TextView
            android:id="@+id/textView4"
            android:layout_
            android:layout_
            android:layout_alignParentEnd="true"
            android:layout_alignTop="@+id/textView3"
            android:layout_marginEnd="43dp"
            android:text="@string/sports2" />

        <ListView
            android:id="@+id/listView3"
            android:layout_
            android:layout_
            android:layout_alignParentStart="true"
            android:layout_below="@+id/textView3"
            android:layout_marginStart="20dp" />

        <ListView
            android:id="@+id/listView4"
            android:layout_
            android:layout_
            android:layout_alignParentEnd="true"
            android:layout_below="@+id/textView4" />
    </RelativeLayout>
</RelativeLayout>

strings.xml

<resources>
    <string name="app_name">Popup</string>
        <string name="news">Check Latest News!</string>
        <string name="local">Local News</string>
        <string name="national">National News</string>
        <string name="sports1">Sports One</string>
        <string name="sports2">Sports Two</string>


        <string-array name="local">
            <item>Star Ledger</item>
            <item>Ny Times</item>
            <item>The Record</item>
            <item>Trentorian</item>

        </string-array>

</resources>

【问题讨论】:

请出示您的代码。 很多帖子都在问如果有代码可以回答的问题,但没有。所以请务必查看How to Ask。 谢谢。我已经添加了代码。 【参考方案1】:

这里是带有两个列表视图和文本视图的弹出菜单示例:

     PopupWindow popupWindow = new PopupWindow(getApplicationContext());

        LayoutInflater inflater = (LayoutInflater) getApplicationContext()
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View layout = inflater.inflate(R.layout.popup_element,
                (ViewGroup) view, false);
        //List first
        TextView nameOfList1 = layout.findViewById(R.id.text1);
        nameOfList1.setText("List one:");
        ListView mListView1 = layout.findViewById(R.id.listView1);
        final String[] data1 = "Value from list 1", "Value from list 2", "Value from list 3", "Value from list 4",
                "Value from list 5", "Value from list 6";
        mListView1.setAdapter(new ArrayAdapter<>(getApplicationContext(), android.R.layout.simple_list_item_1, data1));
        mListView1.setOnItemClickListener(new AdapterView.OnItemClickListener() 
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) 
                Toast.makeText(getApplicationContext(), "You select value from list one " + data1[i], Toast.LENGTH_LONG).show();
            
        );

        //List second
        TextView nameOfList2 = layout.findViewById(R.id.text2);
        nameOfList2.setText("Select planet from list");
        ListView mListView2 = layout.findViewById(R.id.listView2);

        final String[] data2 = "Mercury", "Venus", "Earth", "Mars", "Jupiter", "Saturn", "Uranus", "Neptune";
        mListView2.setAdapter(new ArrayAdapter<>(getApplicationContext(), android.R.layout.simple_list_item_1, data2));
        mListView2.setOnItemClickListener(new AdapterView.OnItemClickListener() 
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) 
                Toast.makeText(getApplicationContext(), "You select plane  " + data2[i], Toast.LENGTH_LONG).show();
            
        );
        popupWindow.setFocusable(true);
        popupWindow.setWidth(WindowManager.LayoutParams.FILL_PARENT);
        popupWindow.setHeight(WindowManager.LayoutParams.WRAP_CONTENT);

        popupWindow.setContentView(layout);
        popupWindow.showAsDropDown(view, 0, 0);

还有popup_element.xml:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/scrollojt"
    android:layout_
    android:layout_
    android:fillViewport="true">

    <LinearLayout
        android:layout_
        android:layout_
        android:orientation="vertical">

        <TextView
            android:id="@+id/text1"
            android:layout_
            android:layout_ />

        <ListView
            android:id="@+id/listView1"
            android:layout_
            android:layout_
            android:background="#f00"></ListView>

        <TextView
            android:id="@+id/text2"
            android:layout_
            android:layout_ />

        <ListView
            android:id="@+id/listView2"
            android:layout_
            android:layout_
            android:layout_below="@+id/listView1"
            android:background="#0f0"></ListView>
    </LinearLayout>
</ScrollView>

【讨论】:

@AkPasaf 请检查这个,它应该可以解决您的问题。【参考方案2】:

问题是您的列表在您的 activity_main 布局中不存在。而是存在于弹出窗口中。

将您的 onCreate() 方法更改为下面的代码,它应该可以正常工作。

protected void onCreate(Bundle savedInstanceState) 
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    //for listView 1
    linearLayout  = (LinearLayout) findViewById(R.id.linear);
    news1 = (Button) findViewById(R.id.news1);
    news1.setOnClickListener(new View.OnClickListener() 
        @Override
        public void onClick(View view) 
            layoutInflater = (LayoutInflater) getApplication().getSystemService(LAYOUT_INFLATER_SERVICE);
            ViewGroup container  = (ViewGroup) layoutInflater.inflate(R.layout.news,null);

            popupWindow = new PopupWindow(container,800,1300,true);
            lst = container.findViewById(R.id.listView1);
            ArrayAdapter<String> adapter = new ArrayAdapter<String>(MainActivity.this,android.R.layout.simple_list_item_1,local);
            lst.setAdapter(adapter);
            lst.setOnItemClickListener(new AdapterView.OnItemClickListener() 
                @Override
                public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) 
                    Toast.makeText(MainActivity.this,local[i],Toast.LENGTH_SHORT).show();
                
            );

            popupWindow.showAsDropDown(news1);
            container.setOnTouchListener(new View.OnTouchListener() 
                @Override
                public boolean onTouch(View view, MotionEvent motionEvent) 
                    popupWindow.dismiss();
                    return false;
                
            );
        
    );


希望这对您有所帮助。 最好的问候,

【讨论】:

欢迎。乐于助人。【参考方案3】:

在你的动作监听器中添加这个:

   news1.setOnClickListener(new View.OnClickListener() 
        @Override
        public void onClick(View view) 
           PopupWindow popupWindow = new PopupWindow(getApplicationContext());

            ArrayList<String> sortList = new ArrayList<>();
            sortList.add("A to Z");
            sortList.add("Z to A");
            sortList.add("Any");
            sortList.add("Test");

            ArrayAdapter<String> adapter = new ArrayAdapter<>(getApplicationContext(), android.R.layout.simple_dropdown_item_1line,
                    sortList);
            ListView listViewSort = new ListView(getApplicationContext());
            listViewSort.setAdapter(adapter);

            // set on item selected
            listViewSort.setOnItemClickListener(new AdapterView.OnItemClickListener() 
                @Override
                public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) 
                    Toast.makeText(getApplicationContext(),
                            "You select in popup menu" + adapterView.getAdapter().getItem(i).toString(), Toast.LENGTH_LONG).show();
                    popupWindow.dismiss();
                
            );

            popupWindow.setFocusable(true);
            popupWindow.setWidth(WindowManager.LayoutParams.FILL_PARENT);
            popupWindow.setHeight(WindowManager.LayoutParams.WRAP_CONTENT);

            popupWindow.setContentView(listViewSort);
            popupWindow.showAsDropDown(view, 0, 0);

            );
        

【讨论】:

我无法使用:/listViewSort.setOnItemClickListener(onItemClickListener());你能把所有的代码放在一个长代码里吗? 谢谢。它工作正常。我必须在弹出窗口中再添加三个 ListView。我可以将 listView 添加到弹出窗口,但无法将其定位在弹出窗口中的唯一位置。你能帮忙吗?

以上是关于如何在 android studio 的弹出窗口内添加滚动的 listView?的主要内容,如果未能解决你的问题,请参考以下文章

如何使用 jquery 禁用 android 键盘的弹出窗口?

尝试在 Android Studio (Java) 中制作弹出窗口/叠加层

在弹出的 Android Studio 中添加视图寻呼机

Android:创建一个具有多个选择选项的弹出窗口

Android:创建一个具有多个选择选项的弹出窗口

在 Android 上的 Cordova 应用程序中的弹出窗口后面模糊