gpt4 book ai didi

java - 拖动时 stub 中的 ListView 变黑。

转载 作者:搜寻专家 更新时间:2023-11-01 09:11:40 28 4
gpt4 key购买 nike

我正在制作一个应用程序,它在底部有一堆按钮,在顶部有一堆 ViewStub。所有按钮都指向一个监听器,该监听器在按钮 id 之间切换,以决定扩充哪个 stub 。因此,如果单击按钮 1,则会撤消之前的任何 stub 加载,并且会扩充 stub 1。

现在,在 stub 4 上,我需要一个 ListView ,但是当我拖动列表时似乎出现了问题。

我遇到的问题是当我拖动 ListView 时它变黑了。

代码如下:包 com.me.myapp;主 Activity .java

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewStub;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;

public class MainActivity extends Activity {
int stubNum;

/** Called when the activity is first created. */
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
if (savedInstanceState == null) {
doStuff((View) findViewById(R.id.button1));
}
}

@Override
protected void onSaveInstanceState(Bundle outState) {
outState.putInt("stubNum", stubNum);
Log.d("Jason", "Saving stubNum is " + stubNum);
// TODO Auto-generated method stub
super.onSaveInstanceState(outState);
}

@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onRestoreInstanceState(savedInstanceState);
stubNum = savedInstanceState.getInt("stubNum");
Log.d("Jason", "Saved stubNum is " + stubNum);
if (findViewById(stubNum) != null)
{
ViewStub stub = (ViewStub) findViewById(stubNum);
View newView = stub.inflate();
Button button1 = (Button) findViewById(R.id.Button01);
button1.setVisibility(Button.INVISIBLE);

Button button2 = (Button) findViewById(R.id.Button02);
button2.setVisibility(Button.VISIBLE);
}
}

// Implement the OnClickListener callback
public void doStuff(View v) {
setContentView(R.layout.main);
switch (v.getId()) {
case R.id.button1:
try
{
stubNum = 1;
ViewStub stub = (ViewStub) findViewById(R.id.stub1);
if (!(stub.getVisibility() == ViewStub.VISIBLE)) {
View newView = stub.inflate();
Button button1 = (Button) findViewById(R.id.Button01);
button1.setVisibility(Button.INVISIBLE);

Button button2 = (Button) findViewById(R.id.Button02);
button2.setVisibility(Button.VISIBLE);
}
stubNum = stub.getId();
Log.d("Jason", "stubNum is " + stubNum);
}
catch(NullPointerException e)
{
e.printStackTrace();
}
break;
case R.id.button2:
try
{
stubNum = 2;
ViewStub stub = (ViewStub) findViewById(R.id.stub2);
if (!(stub.getVisibility() == ViewStub.VISIBLE)) {
View newView = stub.inflate();
Button button1 = (Button) findViewById(R.id.Button01);
button1.setVisibility(Button.INVISIBLE);

Button button2 = (Button) findViewById(R.id.Button02);
button2.setVisibility(Button.INVISIBLE);
}
stubNum = stub.getId();
Log.d("Jason", "stubNum is " + stubNum);
}
catch(NullPointerException e)
{
e.printStackTrace();
}
break;
case R.id.button3:
try
{
stubNum = 3;
ViewStub stub = (ViewStub) findViewById(R.id.stub3);
if (!(stub.getVisibility() == ViewStub.VISIBLE)) {
View newView = stub.inflate();
Button button1 = (Button) findViewById(R.id.Button01);
button1.setVisibility(Button.INVISIBLE);

Button button2 = (Button) findViewById(R.id.Button02);
button2.setVisibility(Button.INVISIBLE);
}
stubNum = stub.getId();
Log.d("Jason", "stubNum is " + stubNum);
}
catch(NullPointerException e)
{
e.printStackTrace();
}
break;
case R.id.button4:
try
{
stubNum = 4;
ViewStub stub = (ViewStub) findViewById(R.id.stub4);
if (!(stub.getVisibility() == ViewStub.VISIBLE)) {
View newView = stub.inflate();
Button button1 = (Button) findViewById(R.id.Button01);
button1.setVisibility(Button.INVISIBLE);

Button button2 = (Button) findViewById(R.id.Button02);
button2.setVisibility(Button.INVISIBLE);
}
stubNum = stub.getId();
Log.d("Jason", "stubNum is " + stubNum);


}
catch(NullPointerException e)
{
e.printStackTrace();
}
break;
case R.id.button5:
try
{
stubNum = 5;
ViewStub stub = (ViewStub) findViewById(R.id.stub5);
if (!(stub.getVisibility() == ViewStub.VISIBLE)) {
View newView = stub.inflate();
Button button1 = (Button) findViewById(R.id.Button01);
button1.setVisibility(Button.INVISIBLE);

Button button2 = (Button) findViewById(R.id.Button02);
button2.setVisibility(Button.INVISIBLE);
}
stubNum = stub.getId();
Log.d("Jason", "stubNum is " + stubNum);
}
catch(NullPointerException e)
{
e.printStackTrace();
}
break;
default:
stubNum = 1;
ViewStub stub = (ViewStub) findViewById(R.id.stub1);
if (!(stub.getVisibility() == ViewStub.VISIBLE)) {
View newView = stub.inflate();
}
break;
}
}
}

主.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout android:id="@+id/relativeLayout1"
android:layout_width="fill_parent" android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout android:layout_height="wrap_content"
android:id="@+id/linearLayout1" android:layout_width="match_parent"
android:layout_alignParentLeft="true"
android:gravity="center" android:layout_alignParentBottom="true">
<ImageButton android:src="@drawable/download" android:id="@+id/button1" android:layout_width="60dp" android:layout_height="40dp" android:layout_margin="2dip" android:onClick="doStuff"></ImageButton>
<ImageButton android:src="@drawable/download" android:id="@+id/button2" android:layout_width="60dp" android:layout_height="40dp" android:layout_margin="2dip" android:onClick="doStuff"></ImageButton>
<ImageButton android:src="@drawable/download" android:id="@+id/button3" android:layout_width="60dp" android:layout_height="40dp" android:layout_margin="2dip" android:onClick="doStuff"></ImageButton>
<ImageButton android:src="@drawable/download" android:id="@+id/button4" android:layout_width="60dp" android:layout_height="40dp" android:layout_margin="2dip" android:onClick="doStuff"></ImageButton>
<ImageButton android:src="@drawable/download" android:id="@+id/button5" android:layout_width="60dp" android:layout_height="40dp" android:layout_margin="2dip" android:onClick="doStuff"></ImageButton>
</LinearLayout>
<RelativeLayout android:id="@+id/relativeLayout2"
android:layout_height="wrap_content" android:layout_width="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true" android:layout_above="@+id/linearLayout1"
android:background="#ffffff" android:layout_below="@+id/relativeLayout3">
<ViewStub android:id="@+id/stub1"
android:inflatedId="@+id/subTree"
android:layout="@layout/myvarsity"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />

<ViewStub android:id="@+id/stub2"
android:inflatedId="@+id/subTree"
android:layout="@layout/livestream"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />

<ViewStub android:id="@+id/stub3"
android:inflatedId="@+id/subTree"
android:layout="@layout/shows"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />

<ViewStub android:id="@+id/stub4"
android:inflatedId="@+id/subTree"
android:layout="@layout/more"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />

<ViewStub android:id="@+id/stub5"
android:inflatedId="@+id/subTree"
android:layout="@layout/settings"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</RelativeLayout>
<RelativeLayout android:id="@+id/relativeLayout3" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:layout_width="match_parent" android:gravity="center_horizontal">
<Button android:id="@+id/Button02" android:text="Customize" android:layout_height="40dp" android:layout_width="wrap_content" android:layout_alignParentTop="true" android:layout_alignParentRight="true"></Button>
<Button android:id="@+id/Button01" android:text="Back" android:layout_height="40dp" android:layout_width="wrap_content" android:layout_alignParentTop="true" android:layout_alignParentLeft="true"></Button>
<TextView android:id="@+id/textView1" android:text="App" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceMedium" android:layout_width="wrap_content" android:layout_centerVertical="true" android:layout_centerHorizontal="true"></TextView>
</RelativeLayout>
</RelativeLayout>

更多.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/relativeLayout1" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:gravity="center_horizontal">
<RelativeLayout android:id="@+id/relativeLayout2"
android:layout_height="wrap_content" android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" android:layout_width="match_parent">
<ListView android:id="@+id/moreList"
android:layout_centerVertical="true" android:layout_width="match_parent"
android:layout_alignParentLeft="true" android:layout_height="match_parent"></ListView>
</RelativeLayout>
<TextView android:id="@+id/textView1" android:textColor="#000000"
android:text="Morelistview" android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:layout_width="wrap_content" android:layout_below="@+id/relativeLayout2"
android:layout_alignParentLeft="true"></TextView>
</RelativeLayout>

more_list_item.xml

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

</RelativeLayout>

最佳答案

android:cacheColorHint = "#00000000"

将此标记用于您的 ListView 。或者你也可以使用

listview.setCacheColorHint()

以编程方式设置它。

关于java - 拖动时 stub 中的 ListView 变黑。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7850300/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com