android - 单击文本视图时显示水平滚动视图
android - show horizontal scrollview when textview is clicked
我是 android 的新手。我正在开发一个应用程序。我有一个 textView
作为 header 和一个 horizontal_scroll_view
的内容。现在,当我按下 textView
时,我需要 horizontal_scroll_view
弹出 header。
XML布局-
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="20dp"
android:paddingStart="20dp"
android:onClick="DailyUseItems"
android:clickable="true"
android:text="Daily Use Products"
android:textColor="#FFFFFF"
android:textSize="24sp" />
<HorizontalScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#EEEEEE"
android:id="@+id/daily_use_items"
android:scrollbars="none">
</HorizontalScrollView>
如果您希望将其显示为弹出窗口,您可以制作自定义对话框 https://developer.android.com/guide/topics/ui/dialogs.html But If you would like to make HorizontalScrollView invisible and make it visible on header click you can set in xml android:visibility="invisible"
or android:visibility="gone"
and then in java click listener horizontalScrollView.setVisibility(View.VISIBLE);
To make it popup/slide down the header you would need to use android animation. Here is how I would do it, based on this code https://gist.github.com/rafali/5146957:
MainActivity.class
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final HorizontalScrollView hsc = (HorizontalScrollView) findViewById(R.id.daily_use_items);
TextView tv = (TextView) findViewById(R.id.header);
hsc.setAlpha(0.0f);
tv.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
hsc.animate().alpha(1.0f);
ResizeAnimation resizeAnimation = new ResizeAnimation(hsc,hsc.getHeight());
resizeAnimation.setDuration(600);
hsc.startAnimation(resizeAnimation);
}
});
}
}
ResizeAnimation.class
public class ResizeAnimation extends Animation {
final int startHeight;
final int targetHeight;
View view;
public ResizeAnimation(View view, int targetHeight) {
this.view = view;
this.targetHeight = targetHeight;
startHeight = 0;
}
@Override
protected void applyTransformation(float interpolatedTime, Transformation t) {
int newHeight = (int) (startHeight + (targetHeight - startHeight) * interpolatedTime);
view.getLayoutParams().height = newHeight;
view.requestLayout();
}
@Override
public void initialize(int width, int height, int parentWidth, int parentHeight) {
super.initialize(width, height, parentWidth, parentHeight);
}
@Override
public boolean willChangeBounds() {
return true;
}
}
我是 android 的新手。我正在开发一个应用程序。我有一个 textView
作为 header 和一个 horizontal_scroll_view
的内容。现在,当我按下 textView
时,我需要 horizontal_scroll_view
弹出 header。
XML布局-
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="20dp"
android:paddingStart="20dp"
android:onClick="DailyUseItems"
android:clickable="true"
android:text="Daily Use Products"
android:textColor="#FFFFFF"
android:textSize="24sp" />
<HorizontalScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#EEEEEE"
android:id="@+id/daily_use_items"
android:scrollbars="none">
</HorizontalScrollView>
如果您希望将其显示为弹出窗口,您可以制作自定义对话框 https://developer.android.com/guide/topics/ui/dialogs.html But If you would like to make HorizontalScrollView invisible and make it visible on header click you can set in xml android:visibility="invisible"
or android:visibility="gone"
and then in java click listener horizontalScrollView.setVisibility(View.VISIBLE);
To make it popup/slide down the header you would need to use android animation. Here is how I would do it, based on this code https://gist.github.com/rafali/5146957:
MainActivity.class
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final HorizontalScrollView hsc = (HorizontalScrollView) findViewById(R.id.daily_use_items);
TextView tv = (TextView) findViewById(R.id.header);
hsc.setAlpha(0.0f);
tv.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
hsc.animate().alpha(1.0f);
ResizeAnimation resizeAnimation = new ResizeAnimation(hsc,hsc.getHeight());
resizeAnimation.setDuration(600);
hsc.startAnimation(resizeAnimation);
}
});
}
}
ResizeAnimation.class
public class ResizeAnimation extends Animation {
final int startHeight;
final int targetHeight;
View view;
public ResizeAnimation(View view, int targetHeight) {
this.view = view;
this.targetHeight = targetHeight;
startHeight = 0;
}
@Override
protected void applyTransformation(float interpolatedTime, Transformation t) {
int newHeight = (int) (startHeight + (targetHeight - startHeight) * interpolatedTime);
view.getLayoutParams().height = newHeight;
view.requestLayout();
}
@Override
public void initialize(int width, int height, int parentWidth, int parentHeight) {
super.initialize(width, height, parentWidth, parentHeight);
}
@Override
public boolean willChangeBounds() {
return true;
}
}