Android:Split the Screen in 2 Equals Parts with 2 Listviews

How to divide a screen equally between a graph and a listview

Remove container that listView stays in, change the weight to 1 and set height of the main container to match_parent. Here is an example:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:id="@+id/graph_container"
android:orientation="vertical">

</LinearLayout>

<ListView
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:fillViewport="true"
android:drawSelectorOnTop="false" >
</ListView>

how to slide two listviews with one touch

I thing it can be achieved using OnScrollListener, essentially you can scroll other list/recyclerview on scroll of it's sibling view.
Here is snippet, assuming you use Recycleview

class MainActivity : AppCompatActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val left = findViewById<RecyclerView>(R.id.left)
val right = findViewById<RecyclerView>(R.id.right)
left.adapter = Adapter()
right.adapter = Adapter()

val switcher = FocusSwitchListener(left, right)

left.addOnScrollListener(object : RecyclerView.OnScrollListener() {
override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
super.onScrolled(recyclerView, dx, dy)
if (switcher.isLeftLastFocused) {
right.stopScroll()
right.scrollBy(dx, dy)
}
}
})
right.addOnScrollListener(object : RecyclerView.OnScrollListener() {
override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
super.onScrolled(recyclerView, dx, dy)
if (switcher.isRightLastFocused) {
left.stopScroll()
left.scrollBy(dx, dy)
}
}
})
}
}

FocusSwitchListener

class FocusSwitchListener(left: View, right: View) {
var isLeftLastFocused: Boolean = false
private set
var isRightLastFocused: Boolean = false
private set

init {
left.setOnTouchListener { _, event ->
if (event?.action == MotionEvent.ACTION_DOWN) {
isLeftLastFocused = true
isRightLastFocused = false
}
false
}
right.setOnTouchListener { _, event ->
if (event?.action == MotionEvent.ACTION_DOWN) {
isRightLastFocused = true
isLeftLastFocused = false
}
false
}
}
}

and activity xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:orientation="horizontal"
android:layout_height="match_parent"
tools:context=".MainActivity">

<androidx.recyclerview.widget.RecyclerView
android:layout_width="0dp"
android:id="@+id/left"
android:layout_weight="0.5"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
android:layout_height="match_parent"/>
<androidx.recyclerview.widget.RecyclerView
android:layout_width="0dp"
android:id="@+id/right"
android:layout_weight="0.5"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
android:layout_height="match_parent"/>
</LinearLayout>

divide screen into two equal parts in flutter

You can Use - MediaQuery to get the size of screen then divide it by 2 to get the First half.

@override
Widget build(BuildContext context) {
return MaterialApp(
title: title,
// theme: ThemeData.light().copyWith(
// platform: _platform ?? Theme.of(context).platform,
// ),
home: DefaultTabController(
length: 3,
child: Scaffold(
// appBar: AppBar(
// title: Text(title),
// ),
body: SafeArea(
child: Column(children: <Widget>[
Container(
color: Colors.greenAccent,
height: MediaQuery.of(context).size.height / 2.2, // Also Including Tab-bar height.
// child: Chewie(
// controller: _chewieController,
// ),
),
PreferredSize(
preferredSize: Size.fromHeight(50.0),
child: TabBar(
labelColor: Colors.black,
tabs: [
Tab(
text: 'One',
),
Tab(
text: 'Two',
),
Tab(
text: 'Three',
)
], // list of tabs
),
),
//TabBarView(children: [ImageList(),])
Expanded(
child: TabBarView(
children: [
Container(
color: Colors.deepOrange,
child: Center(child: Text('Tab1')),
),
Container(
color: Colors.red,
child: Center(child: Text('Tab2')),
),
Container(
color: Colors.yellowAccent,
child: Center(child: Text('Tab3')),
) // class name
],
),
),
])))));
}

Output:

Sample Image

with AppBar - height: MediaQuery.of(context).size.height / 2.5,
Sample Image

with GridView.builder in - TabBarView

Expanded(
child: TabBarView(
children: [
GridView.builder(
itemBuilder: (context, int) {
return CircleAvatar(
backgroundImage: NetworkImage(
'https://placeimg.com/640/480/any'),
);
},
itemCount: 20,
gridDelegate:
SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 3),
shrinkWrap: true,
),
Container(
color: Colors.red,
child: Center(child: Text('Tab2')),
),
Container(
color: Colors.yellowAccent,
child: Center(child: Text('Tab3')),
) // class name
],
),
),

Sample Image

to fetch async data - use - FutureBuilder

    @override
Widget build(BuildContext context) {
return FutureBuilder(
builder: (context,snap){
if(snap.hasData){
return Expanded(
child: GridView.count(
shrinkWrap: true,
childAspectRatio: 2,
scrollDirection: Axis.vertical,
crossAxisCount: 2,
children: new List<Widget>.generate(images.length, (index) {
return buildImage(images[index], context, index);
},
).toList(),
),
);

}
return Center(child: CircularProgressIndicator())
},
future: fetchSubCategoryContentlist(context, 20),
);


Related Topics



Leave a reply



Submit