ورود

View Full Version : چرا لیست ویو کوچیک میشه.مشکل این کد چیه؟



Amin-rz
پنج شنبه 19 تیر 1393, 15:00 عصر
سلام این برنامه ی منه فایل جاوا و xml رو براتون میذارم + اسکرینشات
لطفا یه نگاه بکنید چرا لیست ویو کوچیک میشه در حالی که ارتفاعشو wrap_content گذاشتم.
لیست ویو زیر مجموعه یه اسکرول ویو هست که از دکمه های بالایی تا دکمه های پایینی رو پوشش میده.

کد xml :

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/bg"
tools:context=".MainActivity" >

<LinearLayout
android:id="@+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" >

<ImageButton
android:id="@+id/imageButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/facebook"
android:padding="5dp" />

<ImageButton
android:id="@+id/imageButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/googleplus"
android:padding="5dp" />

<ImageButton
android:id="@+id/imageButton3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/twitter"
android:padding="5dp" />

<ImageButton
android:id="@+id/imageButton4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/home"
android:padding="5dp" />

</LinearLayout>

<ScrollView
android:id="@+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/linearLayout1"
android:layout_above="@+id/linearLayout2" >

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
</ScrollView>

<LinearLayout
android:id="@+id/linearLayout2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true" >

<Button
android:id="@+id/button5"
android:layout_width="140dp"
android:layout_height="65dp"
android:background="@drawable/mainbtnbg2"
android:padding="5dp"
android:text="ارسال اس ام اس"
android:textColor="#ffffff"
android:textStyle="bold" />

<ImageButton
android:id="@+id/imageButton6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/mainbtnbg2"
android:src="@drawable/ic_launcher" />

<ImageButton
android:id="@+id/imageButton7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/mainbtnbg2"
android:src="@drawable/ic_launcher" />

</LinearLayout>
</RelativeLayout>




فایل جاوا :




package com.example.smsbaaz;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;

import android.net.Uri;
import android.os.Bundle;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.Point;
import android.text.Layout;
import android.view.Display;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.webkit.JsResult;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TextView;

public class MainActivity extends Activity {
int onoff;
private ArrayAdapter arrayAdapter;
private static final String[] items = {"lorem", "ipsum", "dolor",
"sit", "amet", "consectetuer", "adipiscing", "elit", "morbi",
"vel", "ligula", "vitae", "arcu", "placerat", "ante",
"porttitor", "sodales", "pellentesque", "augue", "purus"};



@SuppressLint("NewApi")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

ListView list1 = (ListView) findViewById(R.id.listView1);
final ScrollView scrollview1 = (ScrollView) findViewById(R.id.scrollView1);
final Button btn5 = (Button) findViewById(R.id.button5);
final ImageButton ibtn1 = (ImageButton) findViewById(R.id.imageButton1);
final ImageButton ibtn2 = (ImageButton) findViewById(R.id.imageButton2);
final ImageButton ibtn3 = (ImageButton) findViewById(R.id.imageButton3);
final ImageButton ibtn4 = (ImageButton) findViewById(R.id.imageButton4);
final ImageButton ibtn6 = (ImageButton) findViewById(R.id.imageButton6);
final ImageButton ibtn7 = (ImageButton) findViewById(R.id.imageButton7);




//فعالسازی امکانات وب ویو



//تنظیم آداپتر در لیست ویو
arrayAdapter = new ArrayAdapter(this,R.layout.row, R.id.label, items);
list1.setAdapter(arrayAdapter);
//رویداد کلیک روی آیتم های لیست ویو
list1.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position,
long arg3) {
if(position==0)
{Intent smsIntent = new Intent(Intent.ACTION_VIEW);
smsIntent.putExtra("sms_body", "اوکی بود");
smsIntent.putExtra("address", "");
smsIntent.setType("vnd.android-dir/mms-sms");
startActivity(smsIntent);}

}
});






{//home تنظیم
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
final int width = size.x;
int height = size.y;
ibtn1.setLayoutParams(new LinearLayout.LayoutParams(width/4, width/4));
ibtn2.setLayoutParams(new LinearLayout.LayoutParams(width/4, width/4));
ibtn3.setLayoutParams(new LinearLayout.LayoutParams(width/4, width/4));
ibtn4.setLayoutParams(new LinearLayout.LayoutParams(width/4, width/4));
btn5.setLayoutParams(new LinearLayout.LayoutParams(width/2, width/4));
ibtn6.setLayoutParams(new LinearLayout.LayoutParams(width/4, width/4));
ibtn7.setLayoutParams(new LinearLayout.LayoutParams(width/4, width/4));
}



//رویداد کلیک روی دکمه های home

ibtn1.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
});

ibtn2.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
});

ibtn3.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
});


ibtn4.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub

}
});


btn5.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
onoff=1;
// TODO Auto-generated method stub
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
final int width = size.x;
int height = size.y;

scrollview1.setLayoutParams(new LinearLayout.LayoutParams(0,0)) ;

}
});

ibtn6.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
onoff=2;
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
final int width = size.x;
int height = size.y;
scrollview1.setLayoutParams(new LinearLayout.LayoutParams(width,LayoutParams.WRAP_ CONTENT)) ;
}
});


ibtn7.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
final int width = size.x;
int height = size.y;

scrollview1.setLayoutParams(new LinearLayout.LayoutParams(0,0)) ;
}
});


}


private void setListAdapter(ArrayAdapter<String> arrayAdapter) {
// TODO Auto-generated method stub

}


public static String readTextFile(Context ctx, int resId) throws UnsupportedEncodingException
{//تابعی که فایل تکست فارسی را می خواند.
InputStream inputStream = ctx.getResources().openRawResource(resId);

InputStreamReader inputreader = new InputStreamReader(inputStream, "UTF-8");
BufferedReader bufferedreader = new BufferedReader(inputreader);
String line;
StringBuilder stringBuilder = new StringBuilder();
try
{
while ((line = bufferedreader.readLine()) != null)
{
stringBuilder.append(line);
stringBuilder.append("\n");
}
}
catch (IOException e)
{
return null;
} finally {

try {

inputStream.close();
}
catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return stringBuilder.toString();
}


private class MyWebViewClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (Uri.parse(url).getHost().equals("demo.mysamplecode.com")) {
return false;
}
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);
return true;
}
}


private class MyWebChromeClient extends WebChromeClient {

@Override
public boolean onJsAlert(WebView view, String url, String message, JsResult result)
{
new AlertDialog.Builder(view.getContext())
.setMessage(message).setCancelable(true).show();
result.confirm();
return true;
}

}

public class JavaScriptInterface {
Context mContext;

JavaScriptInterface(MainActivity mainActivity) {
mContext = (Context) mainActivity;
}

public void closeMyActivity() {
finish();
}

}


@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;
}
}

smemamian
پنج شنبه 19 تیر 1393, 16:24 عصر
کدهاتون رو داخل تگ xml و java بذارید. خودتون به مبحث نگاه کنید ببینید می تونید چیزی بفهمید ؟!!!

Amin-rz
پنج شنبه 19 تیر 1393, 21:58 عصر
با اديتور ساده نوشتم متنو, با موبايل اومدم.
لطفا تو پست خودتون تو تگ xml و جاوا بذاريد پاسخ بديد ممنون ميشم.

bia2joker
جمعه 20 تیر 1393, 01:08 صبح
من کد شمارو نگاه نکردم ولی وقتی listview را داخل scroll قرار می دهید معمولا این اتفاق می افتد که برای این راه حل اول این تابع را تعریف نمایید

d
public static void setListViewHeightBasedOnChildren(ListView listView) { ListAdapter listAdapter = listView.getAdapter();
if (listAdapter == null)
return;




int desiredWidth = MeasureSpec.makeMeasureSpec(listView.getWidth(), MeasureSpec.UNSPECIFIED);
int totalHeight = 0;
View view = null;
for (int i = 0; i < listAdapter.getCount(); i++) {
view = listAdapter.getView(i, view, listView);
if (i == 0)
view.setLayoutParams(new ViewGroup.LayoutParams(desiredWidth, LayoutParams.WRAP_CONTENT));




view.measure(desiredWidth, MeasureSpec.UNSPECIFIED);
totalHeight += view.getMeasuredHeight();
}
ViewGroup.LayoutParams params = listView.getLayoutParams();
params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
listView.setLayoutParams(params);
listView.requestLayout();
}


بعد از این که adapter لیست خودتونو set کردید از تابع بالا استفاده کنید
کد زیر رو نگاه کنید تا متوجه شوید


ListView listView = (ListView) findViewById(R.id.page_sabt_listview); // Assign adapter to ListView

listView.setAdapter(dataAdapter);
setListViewHeightBasedOnChildren(listView);

Amin-rz
چهارشنبه 08 مرداد 1393, 16:24 عصر
از این تابع استفاده کردم.
برای لیست هایی که طول آیتم های اون برابر هست درست کار می کنه.مثل این:

http://barnamenevis.org/attachment.php?attachmentid=121743&stc=1&d=1406723006

ولی برای لیست هایی مثل لیست یه سری پیامک که طولشون مسلما با هم برابر نیست درست عمل نمی کنه.
مثلا به این عکس نگاه کنید.لیست ویو دیگه پایین تر از این نمیره در حالی که هنوز چند پیامک دیگه مونده.
لطفا یه نگاه بندازید مشکلش کجاست.ممنون میشم.

http://barnamenevis.org/attachment.php?attachmentid=121742&stc=1&d=1406723006

Amin-rz
چهارشنبه 08 مرداد 1393, 20:09 عصر
کسی میدونه چیو باید تغییر بدم؟

SASAN_AH
چهارشنبه 08 مرداد 1393, 20:34 عصر
شما نبايد ليست ويو رو در اسكرول قرار بدي!
اگه ليست بزرگ باشه اسكرول خودش داره پس نيازي به اسكرول نيست

Amin-rz
پنج شنبه 09 مرداد 1393, 10:25 صبح
نياز بود كه اين كارو بكنم راه حلش چيه؟

Amin-rz
جمعه 10 مرداد 1393, 23:26 عصر
Help! . . .:(

harani
شنبه 11 مرداد 1393, 20:35 عصر
کافی از ایم متد استفاده کنی :


public static void updateListViewHeight(ListView myListView) {
ListAdapter myListAdapter = myListView.getAdapter();
if (myListAdapter == null) {
return;
}
// get listview height
int totalHeight = 0;
int adapterCount = myListAdapter.getCount();
for (int size = 0; size < adapterCount; size++) {
View listItem = myListAdapter.getView(size, null, myListView);
listItem.measure(0, 0);
totalHeight += listItem.getMeasuredHeight();
}
// Change Height of ListView
ViewGroup.LayoutParams params = myListView.getLayoutParams();
params.height = totalHeight
+ (myListView.getDividerHeight() * (adapterCount - 1)) + 60;
myListView.setLayoutParams(params);
}