Javascript 嵌套滚动视图开始位置错误,布局较大
我有两个问题: 1. 我使用的是带有嵌套滚动视图的CollasingToolbarLayout,而我的布局内容是 当NestedScrollView中的内容变大时,它会将视图的开始位置移到NestedScrollView中内容的中间位置 更具体地说,问题是在添加gridView之后开始的 外观: 2 在NestedScrollView中,我有一个gridView。这应该包含json响应中的所有图像。图像的数量会有所不同,因此它应该包装内容。但是,当我将其设置为wrap_content时,仅显示第一行,其余行被切断 我如何做到这一点 问题的图像: 我的折叠工具栏布局:Javascript 嵌套滚动视图开始位置错误,布局较大,javascript,android,android-layout,gridview,layout,Javascript,Android,Android Layout,Gridview,Layout,我有两个问题: 1. 我使用的是带有嵌套滚动视图的CollasingToolbarLayout,而我的布局内容是 当NestedScrollView中的内容变大时,它会将视图的开始位置移到NestedScrollView中内容的中间位置 更具体地说,问题是在添加gridView之后开始的 外观: 2 在NestedScrollView中,我有一个gridView。这应该包含json响应中的所有图像。图像的数量会有所不同,因此它应该包装内容。但是,当我将其设置为wrap_content时,仅显示
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:background="#fff3f3f3">
<android.support.design.widget.AppBarLayout
android:id="@+id/app_bar_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
android:fitsSystemWindows="true">
<android.support.design.widget.CollapsingToolbarLayout
android:id="@+id/collapsing_toolbar"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_scrollFlags="scroll|exitUntilCollapsed"
app:contentScrim="#80000000"
app:expandedTitleMarginStart="48dp"
app:expandedTitleMarginEnd="64dp"
android:fitsSystemWindows="true">
<ImageView
android:id="@+id/image"
android:layout_width="match_parent"
android:layout_height="420dp"
android:scaleType="centerCrop"
android:fitsSystemWindows="true"
app:layout_collapseMode="parallax"/>
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
app:layout_collapseMode="pin" />
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<android.support.v4.widget.NestedScrollView
android:id="@+id/scroll"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipToPadding="false"
android:layout_gravity="fill_vertical"
android:overScrollMode="never"
android:layout_marginBottom="?attr/actionBarSize"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<------My NestedScrollView Content is here----->
</android.support.v4.widget.NestedScrollView>
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_marginEnd="5dp"
android:layout_marginRight="5dp"
android:elevation="2dp"
app:layout_anchor="@id/app_bar_layout"
app:layout_anchorGravity="bottom|right|end"
android:src="@drawable/ic_star_white_24dp"
/>
我的NestedScrollView内容:
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="3dp"
android:orientation="vertical"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/background_with_shadow"
android:orientation="vertical"
android:id="@+id/first"
style="@style/Shadow"
android:elevation="1dp"
>
<TextView
android:id="@+id/biography_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="5dp"
android:text="Biography"
android:padding="5dp"
android:textSize="15sp"
android:textColor="@color/PrimaryText"/>
<TextView
android:id="@+id/biography"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="5dp"
android:textSize="12sp"
android:lineSpacingMultiplier="1.2"
android:gravity="start"
android:textColor="@color/SecondaryText"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="@+id/second"
android:background="@drawable/background_with_shadow"
style="@style/Shadow"
android:elevation="1dp">
<TextView
android:id="@+id/companytext1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Images"
android:paddingTop="5dp"
android:paddingLeft="5dp"
android:textSize="15sp"
android:textColor="@color/PrimaryText"/>
<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="fill_parent"
android:layout_height="200dp"
android:overScrollMode="never"
android:padding="5dp"
/>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="150dp"
android:background="@drawable/background_with_shadow"
android:orientation="vertical"
android:id="@+id/thrid"
style="@style/Shadow"
android:elevation="1dp"
android:layout_marginBottom="5dp"
>
<TextView
android:id="@+id/Details_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="5dp"
android:text="Details"
android:padding="5dp"
android:textSize="15sp"
android:textColor="@color/PrimaryText"/>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<TextView
android:id="@+id/birth_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dp"
android:text="Birthday"
android:layout_gravity="start"
android:textSize="12sp"
android:lineSpacingMultiplier="1.2"
android:gravity="start"
android:textColor="#000000"/>
<TextView
android:id="@+id/birth"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dp"
android:layout_toRightOf="@+id/birth_text"
android:layout_toEndOf="@+id/birth_text"
android:layout_gravity="end"
android:textSize="12sp"
android:lineSpacingMultiplier="1.2"
android:gravity="start"
android:textColor="@color/SecondaryText"/>
<TextView
android:id="@+id/Place_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/birth_text"
android:padding="5dp"
android:text="Place of birth"
android:layout_gravity="start"
android:textSize="12sp"
android:lineSpacingMultiplier="1.2"
android:gravity="start"
android:textColor="#000000"/>
<TextView
android:id="@+id/Place"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dp"
android:layout_toEndOf="@+id/Place_text"
android:layout_toRightOf="@+id/Place_text"
android:layout_below="@+id/birth_text"
android:layout_gravity="end"
android:textSize="12sp"
android:lineSpacingMultiplier="1.2"
android:gravity="start"
android:textColor="@color/SecondaryText"/>
<TextView
android:id="@+id/death_text"
android:layout_below="@+id/Place_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dp"
android:text="Deathday"
android:layout_gravity="start"
android:textSize="12sp"
android:lineSpacingMultiplier="1.2"
android:gravity="start"
android:textColor="#000000"/>
<TextView
android:id="@+id/death"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dp"
android:layout_below="@+id/Place_text"
android:layout_toRightOf="@+id/death_text"
android:layout_toEndOf="@+id/death_text"
android:layout_gravity="end"
android:text="-"
android:layout_marginEnd="5dp"
android:layout_marginRight="5dp"
android:textSize="12sp"
android:lineSpacingMultiplier="1.2"
android:gravity="start"
android:textColor="@color/SecondaryText"/>
</RelativeLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="@+id/fourth"
android:background="@drawable/background_with_shadow"
style="@style/Shadow"
android:elevation="1dp">
<TextView
android:id="@+id/grid_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Also known for"
android:paddingTop="5dp"
android:paddingLeft="5dp"
android:textSize="15sp"
android:textColor="@color/PrimaryText"/>
<GridView
android:layout_width="match_parent"
android:layout_height="1000dp"
android:id="@+id/upcoming_gridlayout"
style="@style/Shadow"
android:elevation="1dp"
android:numColumns="2"
>
</GridView>
</LinearLayout>
<View
android:layout_width="wrap_content"
android:layout_height="?attr/actionBarSize">
</View>
</LinearLayout>
我的活动CrewCastSingleItem:
public class CrewCastSingleItem extends Activity {
// create var
String ProfileImage;
String Id;
String Name;
String ProfilePathUrl;
String biography;
String name;
String placeOfBirth;
String deathday;
String birthday;
ImageAdapter adapter;
CrewCastGridViewAdapter Gridadapter;
GridView Gridv;
ArrayList<HashMap<String, String>> mylistForPersonImages;
ArrayList<HashMap<String, String>> mylistForPersonFilmImages;
JSONObject json = null;
JSONArray results = null;
private static final String apiKey = "Mykey";
private static final String tmdbURL = "MyURl";
static final String TAG_POSTERPATH = "poster_path";
static final String TAG_TITLE = "original_title";
static final String TAG_ID = "id";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.cast_single_item);
// Get specific movie data
Intent i = getIntent();
// Get the result of poster
// Get the result of movie details for textviews
ProfileImage = i.getStringExtra("profile_path");
Name = i.getStringExtra("name");
Id = i.getStringExtra("credit_id");
ProfilePathUrl = "http://image.tmdb.org/t/p/w500" + ProfileImage;
// Locate Imageviews
ImageView ImagePoster = (ImageView) findViewById(R.id.image);
// load images url into poster
Picasso.with(getApplicationContext()).load(ProfilePathUrl).into(ImagePoster);
CollapsingToolbarLayout collapsingToolbarLayout = (CollapsingToolbarLayout) findViewById(R.id.collapsing_toolbar);
collapsingToolbarLayout.setTitle(Name) ;
collapsingToolbarLayout.setCollapsedTitleTextColor(Color.parseColor("#ffffff"));
collapsingToolbarLayout.setExpandedTitleTextAppearance(Color.parseColor("#ffffff"));
// Items for download data
//Initialize with empty data
mylistForPersonImages = new ArrayList<HashMap<String, String>>();
mylistForPersonFilmImages = new ArrayList<HashMap<String, String>>();
new DownloadJSON().execute();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_upcoming_singe_item, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
// Downloading data asynchronously
private class DownloadJSON extends AsyncTask<String, String, String> {
@Override
protected String doInBackground(String... url) {
json = JSONfunctions.getJSONfromURL(tmdbURL + "/3/person/" + Id
+ apiKey +"&append_to_response=images,combined_credits");
try {
biography = json.getString("biography");
birthday = json.getString("birthday");
deathday = json.getString("deathday");
name = json.getString("name");
placeOfBirth = json.getString("place_of_birth");
// Get person images
JSONObject images = json.getJSONObject("images");
JSONArray Imagearray = images.getJSONArray("profiles");
for (int i=0; i<Imagearray.length(); i++) {
HashMap<String, String> map = new HashMap<String, String>();
JSONObject actor = Imagearray.getJSONObject(i);
String filepath = actor.getString("file_path");
map.put("file_path", filepath);
mylistForPersonImages.add(map);
}
JSONObject othermovies = json.getJSONObject("combined_credits");
JSONArray otherMoviewarray = othermovies.getJSONArray("cast");
for (int i=0; i<otherMoviewarray.length(); i++) {
HashMap<String, String> map = new HashMap<String, String>();
JSONObject actor = otherMoviewarray.getJSONObject(i);
String filepath = actor.getString(TAG_POSTERPATH);
String title = actor.getString(TAG_TITLE);
String id = actor.getString(TAG_ID);
map.put(TAG_TITLE, title);
map.put(TAG_ID, id);
map.put(TAG_POSTERPATH, filepath);
mylistForPersonFilmImages.add(map);
// Log.v("JSON", "output " + map); // remove before release
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
TextView biographyView = (TextView) findViewById(R.id.biography);
biographyView.setText(biography);
TextView deathView = (TextView) findViewById(R.id.death);
deathView.setText(deathday);
TextView birthView = (TextView) findViewById(R.id.birth);
birthView.setText(birthday);
TextView placeView = (TextView) findViewById(R.id.Place);
placeView.setText(placeOfBirth);
// Locate viewpager
ViewPager viewPager = (ViewPager) findViewById(R.id.viewpager);
adapter = new ImageAdapter(getApplicationContext(), mylistForPersonImages);
viewPager.setAdapter(adapter);
Gridv = (GridView) findViewById(R.id.upcoming_gridlayout);
Gridadapter = new CrewCastGridViewAdapter(getApplicationContext(), mylistForPersonFilmImages);
Gridv.setAdapter(Gridadapter);
}
}
}
公共类CrewCastSingleItem扩展活动{
//创建变量
字符串轮廓图像;
字符串Id;
字符串名;
字符串ProfilePathUrl;
弦乐传记;
字符串名;
出生的字符串位置;
弦死亡日;
弦乐生日;
图像适配器;
CrewCastGridViewAdapter Gridadapter;
GridView-Gridv;
用于人像的阵列列表;
ArrayList mylistForPersonFilmImages;
JSONObject json=null;
JSONArray结果=null;
私有静态最终字符串apiKey=“Mykey”;
私有静态最终字符串tmdbURL=“MyURl”;
静态最终字符串TAG\u POSTERPATH=“poster\u path”;
静态最终字符串标记\u TITLE=“原始\u TITLE”;
静态最终字符串标记\u ID=“ID”;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.cast\u单个项目);
//获取特定的电影数据
Intent i=getIntent();
//得到海报的效果
//获取文本视图的电影详细信息的结果
ProfileImage=i.getStringExtra(“profile_路径”);
名称=i.getStringExtra(“名称”);
Id=i.getStringExtra(“信用卡Id”);
ProfilePathUrl=”http://image.tmdb.org/t/p/w500“+图像;
//定位图像视图
ImageView ImagePoster=(ImageView)findViewById(R.id.image);
//将图像url加载到海报中
Picasso.with(getApplicationContext()).load(ProfilePathUrl).into(ImagePoster);
折叠工具栏布局折叠工具栏布局=(折叠工具栏布局)findViewById(R.id.collading_toolbar);
折叠ToolbarLayout.setTitle(名称);
collapsedtitletextcolor(Color.parseColor(“#ffffff”));
折叠ToolbarLayout.setExpandedTitletExtExtExtaurance(Color.parseColor(“#ffffff”);
//下载数据的项目
//用空数据初始化
mylistForPersonImages=新的ArrayList();
mylistForPersonFilmImages=new ArrayList();
新建下载JSON().execute();
}
@凌驾
公共布尔onCreateOptions菜单(菜单){
//为菜单充气;这会将项目添加到操作栏(如果存在)。
getMenuInflater().充气(R.menu.menu\u即将到来的菜单项,菜单);
返回true;
}
@凌驾
公共布尔值onOptionsItemSelected(菜单项项){
//处理操作栏项目单击此处。操作栏将
//自动处理Home/Up按钮上的点击,只要
//在AndroidManifest.xml中指定父活动时。
int id=item.getItemId();
//noinspection SimplifiableIf语句
if(id==R.id.action\u设置){
返回true;
}
返回super.onOptionsItemSelected(项目);
}
//异步下载数据
私有类下载JSON扩展异步任务{
@凌驾
受保护的字符串doInBackground(字符串…url){
json=JSONfunctions.getJSONfromURL(tmdbURL+“/3/person/”+Id
+apiKey+“&append_to_response=图像,组合积分”);
试一试{
传记=json.getString(“传记”);
生日=json.getString(“生日”);
deathday=json.getString(“deathday”);
name=json.getString(“name”);
placeOfBirth=json.getString(“出生地”);
//获取人物图像
JSONObject images=json.getJSONObject(“images”);
JSONArray Imagearray=images.getJSONArray(“profiles”);
对于(inti=0;i,我遇到了同样的问题。但是,我使用了RecyclerView而不是GridView。
我已通过使用扩展GridLayoutManager的自定义LayoutManager解决了“仅显示第一行”问题:
public class WrappingGridLayoutManager extends GridLayoutManager {
private int[] mMeasuredDimension = new int[2];
private int rowCount;
public WrappingGridLayoutManager(Context context, int spanCount, int itemsNo) {
super(context, spanCount, GridLayoutManager.VERTICAL, false);
rowCount = itemsNo / spanCount;
if (itemsNo % spanCount > 0) {
rowCount = rowCount + 1;
}
}
public WrappingGridLayoutManager(Context context, int spanCount, int itemsNo, int orientation, boolean reverseLayout) {
super(context, spanCount, orientation, reverseLayout);
rowCount = itemsNo / spanCount;
if (itemsNo % spanCount > 0) {
rowCount = rowCount + 1;
}
}
@Override
public void onMeasure(RecyclerView.Recycler recycler, RecyclerView.State state,
int widthSpec, int heightSpec) {
final int widthMode = View.MeasureSpec.getMode(widthSpec);
final int heightMode = View.MeasureSpec.getMode(heightSpec);
final int widthSize = View.MeasureSpec.getSize(widthSpec);
final int heightSize = View.MeasureSpec.getSize(heightSpec);
measureScrapChild(recycler, 0,
View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED),
View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED),
mMeasuredDimension);
int width = mMeasuredDimension[0];
int height = mMeasuredDimension[1];
switch (widthMode) {
case View.MeasureSpec.EXACTLY:
case View.MeasureSpec.AT_MOST:
width = widthSize;
break;
case View.MeasureSpec.UNSPECIFIED:
}
switch (heightMode) {
case View.MeasureSpec.EXACTLY:
case View.MeasureSpec.AT_MOST:
height = heightSize;
break;
case View.MeasureSpec.UNSPECIFIED:
}
height = height * rowCount;
setMeasuredDimension(width, height);
}
private void measureScrapChild(RecyclerView.Recycler recycler, int position, int widthSpec,
int heightSpec, int[] measuredDimension) {
if (getItemCount() > 0) {
View view = recycler.getViewForPosition(position);
if (view != null) {
RecyclerView.LayoutParams p = (RecyclerView.LayoutParams) view.getLayoutParams();
int childWidthSpec = ViewGroup.getChildMeasureSpec(widthSpec,
getPaddingLeft() + getPaddingRight(), p.width);
int childHeightSpec = ViewGroup.getChildMeasureSpec(heightSpec,
getPaddingTop() + getPaddingBottom(), p.height);
view.measure(childWidthSpec, childHeightSpec);
measuredDimension[0] = view.getMeasuredWidth();
measuredDimension[1] = view.getMeasuredHeight();
recycler.recycleView(view);
}
}
}
}
在创建实例时提供项目数,以便它计算视图的高度。然后将其用于recyclerView,如下所示:
recyclerView.setLayoutManager(new WrappingGridLayoutManager(this, 2, items.size()));
recyclerView.setAdapter(new YourAdapter(items));
如果您解决了错误的起始位置问题,请告诉我。谢谢如果有人有见解,请提供指导
public class WrappingGridLayoutManager extends GridLayoutManager {
private int[] mMeasuredDimension = new int[2];
private int rowCount;
public WrappingGridLayoutManager(Context context, int spanCount, int itemsNo) {
super(context, spanCount, GridLayoutManager.VERTICAL, false);
rowCount = itemsNo / spanCount;
if (itemsNo % spanCount > 0) {
rowCount = rowCount + 1;
}
}
public WrappingGridLayoutManager(Context context, int spanCount, int itemsNo, int orientation, boolean reverseLayout) {
super(context, spanCount, orientation, reverseLayout);
rowCount = itemsNo / spanCount;
if (itemsNo % spanCount > 0) {
rowCount = rowCount + 1;
}
}
@Override
public void onMeasure(RecyclerView.Recycler recycler, RecyclerView.State state,
int widthSpec, int heightSpec) {
final int widthMode = View.MeasureSpec.getMode(widthSpec);
final int heightMode = View.MeasureSpec.getMode(heightSpec);
final int widthSize = View.MeasureSpec.getSize(widthSpec);
final int heightSize = View.MeasureSpec.getSize(heightSpec);
measureScrapChild(recycler, 0,
View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED),
View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED),
mMeasuredDimension);
int width = mMeasuredDimension[0];
int height = mMeasuredDimension[1];
switch (widthMode) {
case View.MeasureSpec.EXACTLY:
case View.MeasureSpec.AT_MOST:
width = widthSize;
break;
case View.MeasureSpec.UNSPECIFIED:
}
switch (heightMode) {
case View.MeasureSpec.EXACTLY:
case View.MeasureSpec.AT_MOST:
height = heightSize;
break;
case View.MeasureSpec.UNSPECIFIED:
}
height = height * rowCount;
setMeasuredDimension(width, height);
}
private void measureScrapChild(RecyclerView.Recycler recycler, int position, int widthSpec,
int heightSpec, int[] measuredDimension) {
if (getItemCount() > 0) {
View view = recycler.getViewForPosition(position);
if (view != null) {
RecyclerView.LayoutParams p = (RecyclerView.LayoutParams) view.getLayoutParams();
int childWidthSpec = ViewGroup.getChildMeasureSpec(widthSpec,
getPaddingLeft() + getPaddingRight(), p.width);
int childHeightSpec = ViewGroup.getChildMeasureSpec(heightSpec,
getPaddingTop() + getPaddingBottom(), p.height);
view.measure(childWidthSpec, childHeightSpec);
measuredDimension[0] = view.getMeasuredWidth();
measuredDimension[1] = view.getMeasuredHeight();
recycler.recycleView(view);
}
}
}
}
recyclerView.setLayoutManager(new WrappingGridLayoutManager(this, 2, items.size()));
recyclerView.setAdapter(new YourAdapter(items));