简体   繁体   中英

Transparent/dim ListView background - I want to destroy ListView activity when clicking on background

I have a ListView list that operates nicely. It's set this way:

<style name="Theme.Transparent" parent="android:Theme"> 
    <item name="android:windowIsTranslucent">true </item>
    <item name="android:windowBackground">@android:color/transparent</item>
    <item name="android:windowContentOverlay">@null</item>
    <item name="android:windowNoTitle">true</item>
    <item name="android:windowIsFloating">true</item>
    <item name="android:backgroundDimEnabled">true</item>
</style>

This is my xml file:

<?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="wrap_content"
   android:padding="0dp" 
   android:background="@drawable/button_pressed"
   android:clickable="false"
   android:id="@+id/ListViewLayout" >

    <ImageView
        android:id="@+id/logo"
        android:layout_width="100dp"
        android:layout_height="50dp"
        android:layout_marginLeft="5dp"
        android:layout_marginRight="20dp"
        android:layout_marginTop="5dp" />

    <TextView
        android:id="@+id/label"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textSize="30dp" >
    </TextView>
</LinearLayout> 

I also have a block of code which creates my ListView and handles my clicks:

public class RandomList extends ListActivity {
    static final String[] randomList = new String[] {"Name 1", "Name 2", "Name 3", "Logo"};

    @Override
    public void onCreate (Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setListAdapter(new RandomArrayAdapter(this, randomList));
    }

    protected void onListItemClick(ListView l, View v, int position, long id){
        //Get selected items
        Log.v("ListView l:", l.toString());
        Log.v("View v:", v.toString());
        Log.v("Position:", ""+position);
        Log.v("ID: ", ""+id);
        String selectedValue = (String) getListAdapter().getItem(position);
        Bundle bundle = new Bundle();
        bundle.putString("Name", selectedValue);
        Intent mIntent = new Intent();
        mIntent.putExtras(bundle);
        setResult(RESULT_OK, mIntent);
        finish();
    }
}

Now, with the ArrayAdapter et al, what happens is I get a list of items. As there are only five items, and due to the style I set for it, most of the screen is dimmed out (the original Activity ), and only the list is centred. The problem is, I want to finish the Activity should someone click on the gray region outside of the list box. I cannot for the life of me figure out how.

I tried to implement an OnTouchListener , but ListView isn't letting me do so and I can't figure out how to call it for the LinearLayout container that surrounds the TextView . I'm fairly new to Android, so please dumb it down for me.

Create this xml layout and call it "list_layout.xml":

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

 <ListView android:id="@android:id/list"
           android:layout_width="match_parent"
           android:layout_height="wrap_content"/>
 </LinearLayout>

Then change your onCreate() to the following:

public void onCreate (Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.list_layout);

    View background = findViewById(R.id.my_background);
    background.setOnClickListener(new View.OnClickListener() {
         public void onClick(View v) {
             RamdomList.this.finish();
         }
     });

    setListAdapter(new RandomArrayAdapter(this, randomList));

}

I wrote this off the top of my head without running it, so you might fight a few things off but you get the concept.

I found the solution to my second question, asked regarding the box. In the Theme.Transparent , I just had to turn WindowIsFloating to false . Combine that with Wnafee's excellent answer and it worked like a charm.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM