Hello guys today we are learn about how to use ScrollView in a listview the most problematic android code.Most developers are face this type of problem so lets start and enjoy...
activity_main.xml
ListViewAdapter.java
item.xml
Thank you
i hope very helpful this blog
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="19dp"
android:gravity="center"
android:text="@string/name2" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingLeft="24dp"
android:paddingRight="24dp"
android:paddingTop="56dp">
<ImageView
android:layout_width="wrap_content"
android:layout_height="72dp"
android:layout_gravity="center_horizontal"
android:layout_marginBottom="24dp"
android:src="@drawable/ic_account" />
<!-- Email Label -->
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginTop="8dp">
<EditText
android:id="@+id/input_email"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Email"
android:inputType="textEmailAddress" />
</android.support.design.widget.TextInputLayout>
<!-- Password Label -->
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginTop="8dp">
<EditText
android:id="@+id/input_password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Password"
android:inputType="textPassword" />
</android.support.design.widget.TextInputLayout>
<android.support.v7.widget.AppCompatButton
android:id="@+id/btn_login"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="24dp"
android:layout_marginTop="24dp"
android:padding="12dp"
android:text="Login" />
<TextView
android:id="@+id/link_signup"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="24dp"
android:gravity="center"
android:text="No account yet? Create one"
android:textSize="16dip" />
</LinearLayout>
<ListView
android:id="@+id/listView"
android:layout_width="fill_parent"
android:layout_height="match_parent" />
</LinearLayout>
</ScrollView>
ActivityMain.java
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="19dp"
android:gravity="center"
android:text="@string/name2" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingLeft="24dp"
android:paddingRight="24dp"
android:paddingTop="56dp">
<ImageView
android:layout_width="wrap_content"
android:layout_height="72dp"
android:layout_gravity="center_horizontal"
android:layout_marginBottom="24dp"
android:src="@drawable/ic_account" />
<!-- Email Label -->
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginTop="8dp">
<EditText
android:id="@+id/input_email"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Email"
android:inputType="textEmailAddress" />
</android.support.design.widget.TextInputLayout>
<!-- Password Label -->
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginTop="8dp">
<EditText
android:id="@+id/input_password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Password"
android:inputType="textPassword" />
</android.support.design.widget.TextInputLayout>
<android.support.v7.widget.AppCompatButton
android:id="@+id/btn_login"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="24dp"
android:layout_marginTop="24dp"
android:padding="12dp"
android:text="Login" />
<TextView
android:id="@+id/link_signup"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="24dp"
android:gravity="center"
android:text="No account yet? Create one"
android:textSize="16dip" />
</LinearLayout>
<ListView
android:id="@+id/listView"
android:layout_width="fill_parent"
android:layout_height="match_parent" />
</LinearLayout>
</ScrollView>
ActivityMain.java
public class MainActivity extends AppCompatActivity {
private List<String> list;
private ListViewAdapter listAdapter;
ListView listView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
list= Constant.getList();
listView = (ListView) findViewById(R.id.listView);
listAdapter=new ListViewAdapter(this,list);
listView.setAdapter(listAdapter);
Constant.expandListViewSize(listView);
}
}
private List<String> list;
private ListViewAdapter listAdapter;
ListView listView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
list= Constant.getList();
listView = (ListView) findViewById(R.id.listView);
listAdapter=new ListViewAdapter(this,list);
listView.setAdapter(listAdapter);
Constant.expandListViewSize(listView);
}
}
ListViewAdapter.java
public class ListViewAdapter extends BaseAdapter {
private Context context;
private List<String> listdata;
public ListViewAdapter(Context ctx, List<String> list)
{
this.context=ctx;
this.listdata=list;
}
@Override
public int getCount() {
return listdata.size();
}
@Override
public Object getItem(int position) {
return listdata.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater layoutInflater=LayoutInflater.from(context);
MyHolder myHolder=new MyHolder();
if (convertView==null)
{
convertView =layoutInflater.inflate(R.layout.item, parent, false);
myHolder.item= (TextView) convertView.findViewById(R.id.item);
convertView.setTag(myHolder);
}else
{
myHolder= (MyHolder) convertView.getTag();
}
myHolder.item.setText(listdata.get(position));
return convertView;
}
public class MyHolder
{
TextView item;
}
}
private Context context;
private List<String> listdata;
public ListViewAdapter(Context ctx, List<String> list)
{
this.context=ctx;
this.listdata=list;
}
@Override
public int getCount() {
return listdata.size();
}
@Override
public Object getItem(int position) {
return listdata.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater layoutInflater=LayoutInflater.from(context);
MyHolder myHolder=new MyHolder();
if (convertView==null)
{
convertView =layoutInflater.inflate(R.layout.item, parent, false);
myHolder.item= (TextView) convertView.findViewById(R.id.item);
convertView.setTag(myHolder);
}else
{
myHolder= (MyHolder) convertView.getTag();
}
myHolder.item.setText(listdata.get(position));
return convertView;
}
public class MyHolder
{
TextView item;
}
}
item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/item"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="item" />
</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/item"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="item" />
</LinearLayout>
Thank you
i hope very helpful this blog
This comment has been removed by the author.
ReplyDelete