我有一个listview,它使用一个接收字符串数组作为参数的适配器。当我查看列表视图时,它会显示1个项目,然后它会在其后面显示很多空白区域,如果我向下滚动,则会显示下一个项目。可能是什么问题?
class CustomAdapter extends ArrayAdapter<String> {
CustomAdapter(Context context, String[] camere) {
super(context, R.layout.custom_row, camere);
}
boolean parola = true;
boolean intra = true;
String player_id, room_id;
String siteul = "some_site";
String site;
HashMap<String, String> hash;
static class ViewHolder {
TextView camera;
TextView players;
TextView max_players;
ImageView privata;
Button Buton;
String room_id, nume;
}
ViewHolder ceva;
@Override
public View getView(int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
String variabile[] = getItem(position).split("\\s+");
if(convertView == null)
{
LayoutInflater linflater = LayoutInflater.from(getContext());
convertView = linflater.inflate(R.layout.custom_row, parent, false);
holder = new ViewHolder();
holder.camera = (TextView) convertView.findViewById(R.id.Nume);
holder.players = (TextView) convertView.findViewById(R.id.players);
holder.max_players = (TextView) convertView.findViewById(R.id.max_players);
holder.privata = (ImageView) convertView.findViewById(R.id.privata);
holder.Buton = (Button) convertView.findViewById(R.id.Buton);
holder.camera.setText(variabile[0]);
if (!variabile[1].equals("true")) {
parola = false;
holder.privata.setVisibility(View.INVISIBLE);
}
holder.players.setText(variabile[2]);
holder.max_players.setText(variabile[3]);
holder.room_id = variabile[4];
holder.nume = variabile[5];
holder.Buton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
int playeri = Integer.parseInt(holder.players.getText().toString());
int maximi = Integer.parseInt(holder.max_players.getText().toString());
if(playeri < maximi)
{
hash = new HashMap<String, String>();
hash.put("name", holder.nume);
hash.put("room", holder.room_id);
if (intra) {
holder.Buton.setText("Iesi");
site = siteul + "/join";
intra = false;
} else {
holder.Buton.setText("Intra");
site = siteul + "/leave";
intra = true;
}
new ATask((ViewHolder) v.getTag()).execute(site);
}
}
});
convertView.setTag(holder);
holder.Buton.setTag(holder);
}
else
holder = (ViewHolder) convertView.getTag();
return convertView;
}
public void CheckStart()
{
site = siteul + "/checkstart";
hash = new HashMap<String, String>();
hash.put("room", room_id);
new ATask(ceva).execute(site);
}
public void CheckPlayers()
{
site = siteul + "/checkplayers";
hash = new HashMap<String, String>();
hash.put("room", room_id);
new ATask(ceva).execute(site);
}
public void openGame(String litera)
{
Intent incercare = new Intent(getContext(), Game.class);
incercare.putExtra("nume", player_id);
incercare.putExtra("room", room_id);
incercare.putExtra("litera", litera);
incercare.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
getContext().startActivity(incercare);
}
private String getPostDataString(HashMap<String, String> params) throws UnsupportedEncodingException {
StringBuilder result = new StringBuilder();
boolean first = true;
for(Map.Entry<String, String> entry : params.entrySet()){
if (first)
first = false;
else
result.append("&");
result.append(URLEncoder.encode(entry.getKey(), "UTF-8"));
result.append("=");
result.append(URLEncoder.encode(entry.getValue(), "UTF-8"));
}
return result.toString();
}
public class ATask extends AsyncTask<String, Void, String> {
String rez = "";
ViewHolder myHolder;
public ATask(ViewHolder view) {
myHolder = view;
ceva = myHolder;
room_id = myHolder.room_id;
}
@Override
protected String doInBackground(String... urls) {
//try {
try {
Log.e("rasp", site);
URL obj = new URL(site);
try {
Log.e("rasp", obj.toString());
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
con.setRequestMethod("POST");
con.setRequestProperty("Content-Type",
"application/x-www-form-urlencoded");
//con.setRequestProperty("User-Agent", USER_AGENT);
// For POST only - START
con.setDoOutput(true);
OutputStream os = con.getOutputStream();
os.write(getPostDataString(hash).getBytes());
os.flush();
os.close();
// For POST only - END
int responseCode = con.getResponseCode();
Log.e("rasp", "response code-ul e " + Integer.toString(responseCode));
if (responseCode == HttpURLConnection.HTTP_OK) { //success
BufferedReader in = new BufferedReader(new InputStreamReader(
con.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
// print result
rez = response.toString();
}
else {
Log.e("rasp", "POST request not worked");
}
} catch (IOException e) {
e.printStackTrace();
}
} catch (MalformedURLException e) {
Log.e("naspa", "E corupt!");
}
//} catch (Exception e) {
// Log.e("rasp", "aia e");
//}
return rez;
}
// onPostExecute displays the results of the AsyncTask.
@Override
protected void onPostExecute(String result) {
if (rez.charAt(0) == 'Y')
openGame(rez.substring(2));
else if (rez.charAt(0) == 'V' && !intra)
{
TextView players_mare = myHolder.players;
players_mare.setText(rez.substring(2));
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
// Do something after 1s = 1000ms
CheckStart();
}
}, 1000);
}
else if(rez.charAt(0) == 'G')
CheckPlayers();
else if(rez.charAt(0) == 'Z')
{
TextView players_mare = myHolder.players;
players_mare.setText(rez.substring(2));
}
else if (rez.charAt(0) == 'D')
{
player_id = rez.substring(2);
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
// Do something after 1s = 1000ms
CheckStart();
}
}, 400);
}
}
}
}
这是“custom_row.xml”的xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/background_joc">
<ScrollView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginTop="5dp"
android:id="@+id/scrollView">
<LinearLayout
android:background="@color/grey"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:layout_marginTop="10dp"
android:layout_marginLeft="7dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Nume camera"
android:id="@+id/Nume"
android:textSize="25dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Runde:"
android:layout_marginTop="-7dp"
android:layout_marginLeft="3dp"
android:id="@+id/textView15" />
<TextView
android:layout_marginTop="15dp"
android:layout_marginLeft="-30dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="5"
android:id="@+id/runde"
android:textSize="25dp" />
<ImageView
android:layout_marginTop="18dp"
android:layout_marginLeft="20dp"
android:background="@drawable/buton_lock"
android:layout_width="15dp"
android:layout_height="20dp"
android:id="@+id/privata" />
<TextView
android:layout_marginTop="10dp"
android:layout_marginLeft="7dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="0"
android:id="@+id/players"
android:textSize="30dp" />
<TextView
android:layout_marginTop="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="? android:attr/textAppearanceMedium"
android:text="/"
android:id="@+id/nu_trebuia_sa_aiba_id"
android:textSize="30dp" />
<TextView
android:layout_marginTop="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="5"
android:id="@+id/max_players"
android:textSize="30dp" />
<Button
android:layout_marginTop="10dp"
android:layout_width="90dp"
android:layout_height="40dp"
android:text="Intra"
android:id="@+id/Buton"
android:layout_marginLeft="5dp" />
<TextView
android:layout_marginLeft="50dp"
android:layout_marginTop="50dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text=""
android:id="@+id/e_sa_pacaleasca_match_parentu_de_la_linear_layout"
/>
</LinearLayout>
</ScrollView>
答案 0 :(得分:0)
从R.layout.custom_row
我们可以看到您创建了根视图(RelativeLayout
)填充父级并使ScrollView
match_parent
的高度变为高度。所以Scrollview
占据了整个屏幕。空白区域属于scrollview
。我认为你可以删除scrollview
并使根视图(RelativeLayout
)包装内容。
尝试以下代码:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/background_joc">
<ScrollView
android:id="@+id/scrollView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@color/grey"
android:orientation="horizontal">
<TextView
android:id="@+id/Nume"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="7dp"
android:layout_marginTop="10dp"
android:text="Nume camera"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textSize="25dp"/>
<TextView
android:id="@+id/textView15"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="3dp"
android:layout_marginTop="-7dp"
android:text="Runde:"
android:textAppearance="?android:attr/textAppearanceSmall"/>
<TextView
android:id="@+id/runde"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="-30dp"
android:layout_marginTop="15dp"
android:text="5"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textSize="25dp"/>
<ImageView
android:id="@+id/privata"
android:layout_width="15dp"
android:layout_height="20dp"
android:layout_marginLeft="20dp"
android:layout_marginTop="18dp"
android:background="@drawable/buton_lock"/>
<TextView
android:id="@+id/players"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="7dp"
android:layout_marginTop="10dp"
android:text="0"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textSize="30dp"/>
<TextView
android:id="@+id/nu_trebuia_sa_aiba_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="/"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textSize="30dp"/>
<TextView
android:id="@+id/max_players"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="5"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textSize="30dp"/>
<Button
android:id="@+id/Buton"
android:layout_width="90dp"
android:layout_height="40dp"
android:layout_marginLeft="5dp"
android:layout_marginTop="10dp"
android:text="Intra"/>
<TextView
android:id="@+id/e_sa_pacaleasca_match_parentu_de_la_linear_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="50dp"
android:layout_marginTop="50dp"
android:text=""
android:textAppearance="?android:attr/textAppearanceSmall" />
</LinearLayout>
</ScrollView>
</RelativeLayout>
答案 1 :(得分:0)
在custom_row.xml
:
将layout_height
,RelativeLayout
和ScrollView
的{{1}}从LinearLayout
更改为fill_parent
。